1

我想使用按钮更改图像在三个不同位置的位置...使用我的代码,图像仅移动一个位置...

视图控制器.h

@property (weak, nonatomic) IBOutlet UIImageView *Switch3Way;

- (IBAction)Switch3WayPressed:(id)sender;

视图控制器.m

- (void)Switch3WayPressed:(id)sender {
CGRect frame = Switch3Way.frame;
frame.origin.x = 323;
frame.origin.y = 262;
Switch3Way.frame = frame;

}

4

2 回答 2

1

您能否具体说明为什么需要将按钮移动到三个不同的位置?无论如何,我希望你会根据一些逻辑进行更改 - 所以在头文件中定义三个枚举,例如如下所示:

typedef enum {
buttonState1,
buttonState2,
buttonState3
} buttonState;

然后,根据您的业务逻辑要求,将这些枚举变量设置在代码中的适当位置,例如:

-(void)setButtonState{

buttonState = buttonState1;

}

现在,在您的按钮 touch-up-inside 处理程序例程中,使用 switch 语句来设置适当的框架,例如,如下所示:

- (void)Switch3WayPressed:(id)sender {
    if (![sender isKindOfClass:[Switch3Way class]])
    return;
 switch (buttonState)

 {
 case buttonState1:
      {
      CGRect frame = Switch3Way.frame;
      frame.origin.x = 323;
      frame.origin.y = 262;
      Switch3Way.frame = frame;
      break;
   }
 case buttonState2:
      //some other rect origin based on your logic
      break;
 case buttonState3:
      //some other rect origin based on your logic
      break;
 default:
      break;

 }
于 2013-10-19T00:18:11.393 回答
1

以下代码假定您要为 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 创建动画的好方法。

于 2013-10-19T21:21:02.493 回答