UIImageView动画从左到右

Was*_*sim 1 cocoa-touch core-animation objective-c uiimageview ios

我想把我UIImageView从左到右移动,反之亦然.我使用以下代码完成了一半:

[UIView setAnimationDuration:1.0];
[UIView setAnimationRepeatCount:10];
[UIView setAnimationRepeatAutoreverses:YES];

CGPoint pos = mover.center;
pos.x = 100.0f;
mover.center = pos;

[UIView commitAnimations];
Run Code Online (Sandbox Code Playgroud)

哪里moverUIImageView.我面临的问题是我无法完全从左向右移动它.上面的代码只是从右到中移动它.我想从中心向左走.有人可以指导我吗?

Dee*_*olu 6

我不认为UIKit动画会为您提供直接的关键帧动画来获得振荡效果.我们可以尝试通过一个接一个地触发一个动画来使用委托来实现它,但它没有效率CAKeyframeAnimation.要使用它,您必须QuartzCore在项目中包含框架#import <QuartzCore/QuartzCore.h>.你可以通过做这样的事情来达到你的振荡效果,

self.mover.center = CGPointMake(160, 240);

CAKeyframeAnimation *animation;

animation = [CAKeyframeAnimation animationWithKeyPath:@"position.x"];
animation.duration = 3.0f;
animation.repeatCount = 10;
animation.values = [NSArray arrayWithObjects:
                    [NSNumber numberWithFloat:160.0f],
                    [NSNumber numberWithFloat:320.0f],
                    [NSNumber numberWithFloat:160.0f],
                    [NSNumber numberWithFloat:0.0f],
                    [NSNumber numberWithFloat:160.0f], nil]; 
animation.keyTimes = [NSArray arrayWithObjects:
                      [NSNumber numberWithFloat:0.0],
                      [NSNumber numberWithFloat:0.25],
                      [NSNumber numberWithFloat:.5], 
                      [NSNumber numberWithFloat:.75],
                      [NSNumber numberWithFloat:1.0], nil];    

animation.removedOnCompletion = NO;

[self.mover.layer addAnimation:animation forKey:nil];
Run Code Online (Sandbox Code Playgroud)

这段代码会从左到右摆动一个非常接近你的描述的视图,虽然为了获得你想要的确切效果,你可能需要稍微改变一下.