开发者

UIView: rotation + movement

I'm trying to move and rotate a UIImageView. I'm not using Core Animation for this nor I can use it.

By the way, the core part of the animation is what follows:

CGRect r = self.frame;
r.origin.y -= self.gravity + spring;
r.origin.x -= 1;
self.rotation -= 0.015;
self.transform = CGAffineTransformMakeRotation(rotation);
self.frame = r;

It almost works. I does move or it does rotate in place, but when I try to move and rotate it, it completely deforms skewing.

I'开发者_Python百科ve tried to move and then rotate and viceversa. I also tried various values for content mode with no luck. Any help?

EDIT

useful tutorial: http://www.permadi.com/blog/2009/05/iphone-sdk-bouncing-and-rotating-ball-example/


It's hard to be sure what's going on without a picture of what you are seeing, but the following may be useful:

When you apply a rotation to a view it rotates it around (0,0), so you need to make sure you set the bounds of your view so that (0,0) is where you want the center of rotation. Suppose the image you are rotating is a 100x200 image and you want to rotate around the center of the image then you would need to say self.bounds = CGMakeRect(-50, -100, 100, 200); If you don't do this it will rotate about it's upper-left corner making sort of a spiral.

Oh, and using self.center to set the view's position seems to be more predictable than using self.frame when you have applied a rotation.

Here's some demo code that might make it easier to understand. In this case the code that does the animation is the view controller, but you should get the idea:

#define USE_SELF_CENTER 0

- (void)viewDidLoad {
    [super viewDidLoad];
    self.rotation = 0.0f;
    self.translation = CGPointMake(self.view.bounds.size.width/2.0f, 0.0);

    ObjectView* ov = [[ObjectView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 100.0f, 100.0f)];
#if USE_SELF_CENTER==1
    ov.center = self.translation;
#else
    ov.transform = CGAffineTransformMakeTranslation(self.translation.x, self.translation.y);
#endif
    // adjust bounds for center of rotation
    ov.bounds = CGRectMake(-50.0f, -50.0f, 100.0f, 100.0f);
    [self.view addSubview:ov];
    self.objectView = ov;
    [ov release];

    NSTimer *aTimer = [NSTimer timerWithTimeInterval:0.5 target:self selector:@selector(nextFrame)
                                            userInfo:nil repeats:YES];
    self.timer = aTimer;
    [aTimer  release];
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}



- (void)nextFrame
{
    NSLog(@"nextFrame");
    self.rotation += (5.0 / 180.0f * M_PI);
    self.translation = CGPointMake(self.translation.x, self.translation.y + 2.0);

#if USE_SELF_CENTER==1
    CGAffineTransform t = CGAffineTransformMakeRotation(self.rotation);
    self.objectView.center = self.translation;
    self.objectView.transform = t;
#else
    CGAffineTransform rot = CGAffineTransformMakeRotation(self.rotation);
    CGAffineTransform txy = CGAffineTransformMakeTranslation(self.translation.x, self.translation.y);
    CGAffineTransform t = CGAffineTransformConcat(rot, txy);
    self.objectView.transform = t;
#endif
}

The code demonstrates two ways of doing what you want. If USE_SELF_CENTER is zero then all the animation is done via transforms. If USE_SELF_CENTER is non-zero a transform is used for rotation and the translation is accomplished by setting the center of the view.


use something like this, the idea is combine both the transformation i.e.

CGAffineTransform transforms = CGAffineTransformConcat(CGAffineTransformMakeTranslation(-1, -(self.gravity + spring)),CGAffineTransformMakeRotation(rotation));
self.transform = transforms;


Hope this will work


Hello Friend please use this steps. On your XIB, take UIImageView and set Image and set its IBOutlet.

In .h file

 int theta;
 NSTimer *tmrAnimation;
 IBOutlet UIImageView *imgViewLoader;

In .m file

 - (void)viewDidLoad 
 {
     [super viewDidLoad];    
     theta = 0;
     tmrAnimation = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(animationImageViewLoader) userInfo:nil repeats:YES];
 }

Add This function in your .m file.

 -(void)animationImageViewLoader
 {
     CGFloat angle = theta * (PI / 100);
     CGAffineTransform transform = CGAffineTransformMakeRotation(angle);
     theta = (theta + 1) % 200;
     imgViewLoader.transform = transform;
 }

This code will help for Image Rotation only....


Following code helped me in achieving translation(vertical movement) and rotation together.

CGAffineTransform transform = CGAffineTransformMakeTranslation(0, -200); transform = CGAffineTransformRotate(transform, M_PI);

[UIView transitionWithView:self
                  duration:0.5
                   options:UIViewAnimationOptionAllowAnimatedContent<br>
                animations:^{
                    [view setTransform:transform];
                }
                    completion:^(BOOL finished){
                }];

Hope this will help someone else...

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜