简体   繁体   English

用动画移动UIImageView

[英]Move a UIImageView with an animation

I have implemented a slider bar type UI control, like the Unlock mechanism on the iPhone. 我已经实现了滑块类型的UI控件,例如iPhone上的Unlock机制。

When the touchesEnded method gets called, I return the slider to it's starting position. 当touchesEnded方法被调用时,我将滑块返回到其起始位置。 Instead, I would like to animate its move back to it's starting position. 取而代之的是,我想为其返回初始位置设置动画。

The code should be something like this, but it doesn't work. 该代码应该是这样的,但是它不起作用。 What am I doing wrong here? 我在这里做错了什么?

Self.unlock is a UIImageView: Self.unlock是一个UIImageView:

CABasicAnimation *theAnimation;
theAnimation = [CABasicAnimation animationWithKeyPath:@"position.x"];
theAnimation.fromValue = [NSNumber numberWithFloat:BEGINX];
theAnimation.toValue = [NSNumber numberWithFloat: 0.0];
theAnimation.duration = 0.5;
[self.unlock addAnimation: theAnimation];

Sorry for the bone-headed question! 很抱歉这个头脑呆滞的问题!

You can probably do this in a much simpler way using implicit animation. 您可以使用隐式动画以更简单的方式执行此操作。 Assuming you currently are doing something like this: 假设您当前正在执行以下操作:

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  ...
  myView.frame = startingFrame;
  ...
}

You can use the implicit animator to animate any changes you make to the view (such as its frame): 您可以使用隐式动画器来动画化对视图所做的任何更改(例如其框架):

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
  ...
  [UIView beginAnimations:nil context:nil];
  [UIView setAnimationDuration:0.5];
  myView.frame = startingFrame;
  [UIView commitAnimations];
  ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM