繁体   English   中英

用动画移动UIImageView

[英]Move a UIImageView with an animation

我已经实现了滑块类型的UI控件,例如iPhone上的Unlock机制。

当touchesEnded方法被调用时,我将滑块返回到其起始位置。 取而代之的是,我想为其返回初始位置设置动画。

该代码应该是这样的,但是它不起作用。 我在这里做错了什么?

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];

很抱歉这个头脑呆滞的问题!

您可以使用隐式动画以更简单的方式执行此操作。 假设您当前正在执行以下操作:

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

您可以使用隐式动画器来动画化对视图所做的任何更改(例如其框架):

- (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