我想用一个按钮在三个不同的位置改变图像的位置...用我的代码图像只移动了一个位置...
ViewController.h
@property (weak, nonatomic) IBOutlet UIImageView *Switch3Way;
- (IBAction)Switch3WayPressed:(id)sender;
ViewController.m
- (void)Switch3WayPressed:(id)sender {
CGRect frame = Switch3Way.frame;
frame.origin.x = 323;
frame.origin.y = 262;
Switch3Way.frame = frame;
}
下面的代码假设您希望对Switch3Way UIImageView IBOutlet属性进行动画化。此代码段将把您的UIImageView移动到三个不同的位置,并在最后一个位置停止动画。
#import <QuartzCore/QuartzCore.h>
-(IBAction)move:(id)sender
{
CGPoint firstPosition = CGPointMake(someXvalue, someYvalue);
CGPoint secondPosition = CGPointMake(someXvalue, someYvalue);
CGPoint thirdPosition = CGPointMake(someXvalue, someYvalue);
CABasicAnimation *posOne = [CABasicAnimation animationWithKeyPath:@"position"];
posOne.fromValue = [NSValue valueWithCGPoint:_Switch3Way.layer.position];
posOne.toValue = [NSValue valueWithCGPoint:firstPosition];
posOne.beginTime = 0;
posOne.duration = 1;
CABasicAnimation *posTwo = [CABasicAnimation animationWithKeyPath:@"position"];
posTwo.fromValue = [NSValue valueWithCGPoint:firstPosition];
posTwo.toValue = [NSValue valueWithCGPoint:secondPosition];
posTwo.beginTime = 1;
posTwo.duration = 1;
CABasicAnimation *posThree = [CABasicAnimation animationWithKeyPath:@"position"];
posThree.fromValue = [NSValue valueWithCGPoint:secondPosition];
posThree.toValue = [NSValue valueWithCGPoint:thirdPosition];
posThree.beginTime = 2;
posThree.duration = 1;
CAAnimationGroup *anims = [CAAnimationGroup animation];
anims.animations = [NSArray arrayWithObjects:posOne, posTwo, posThree, nil];
anims.duration = 3;
anims.fillMode = kCAFillModeForwards;
anims.removedOnCompletion = NO;
[_Switch3Way.layer addAnimation:anims forKey:nil];
_Switch3Way.layer.position = thirdPosition;
}
Invasivecode有一个关于创建动画的非常好的系列教程,您可以参考http://weblog.Invasivecode.com/post/4448661320/core-animation-part-iii-basic-animations您最终会希望使用CAKeyframeAnimation对象来创建这些类型的动画,但是了解CABasicAnimations是开始使用coreanimation创建动画的一个好方法。