【问题标题】:Change image position with Button使用按钮更改图像位置
【发布时间】:2013-10-27 22:49:42
【问题描述】:

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

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;

}

【问题讨论】:

  • 那么你的问题是什么?该代码有效吗?你得到了什么结果?
  • 使用我的代码,图像仅移动了一个位置...
  • 你的意思是你的代码图像只移动到一个位置?并且要将图像移动到其他两个位置?
  • 是的,在三个不同的位置...

标签: ios button uiview position


【解决方案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;

 }

【讨论】:

  • 编写代码时会发生这些错误... buttonState = buttonState1; - 预期标识符或'(' - 开关 (buttonState) - 意外类型名称'按钮状态':预期表达式...
  • @Subzero,我想将图像移动三个位置的原因是为了创建一个简单的动画......
【解决方案2】:

以下代码假定您要为 Switch3Way UIImageView IBOutlet 属性设置动画。这段代码 sn-p 会将你的 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 创建动画的好方法.

【讨论】:

    猜你喜欢
    • 2020-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多