始终将UIView设置为用户的点击位置

Rit*_*its 1 iphone cocoa-touch core-animation objective-c uiview

我有一个UIView,我想动画到用户手指当前所在的位置.

因此,当触摸开始时,它必须设置为点击位置的动画.

当触摸移动时,它必须将其目的地调整到新位置.

触摸结束时,必须将动画结束到最后一个点击位置.

我尝试了几件事但没有成功.我尝试使用beginFromCurrentState创建一个新动画touchesMoved,但这也不起作用.

Mat*_*ong 5

看起来你有点复杂了.如果要实现touchesMoved,则需要拖动.如果您对点击更感兴趣,那么touchesEnded就是您要实现此目的的地方.无论哪种方式,您只需要查看位置并根据该位置更新您的UIView中心.如果将更改包装到UIView动画块中的中心值,它将在更新时动画到该位置.

如果您使用的是iOS 3.2或更高版本,则可以使用手势识别器,这使得这非常简单:

// In viewDidLoad or some such
UITapGestureRecognizer *tapRecognizer = [[UITapGestureRecognizer alloc]
                         initWithTarget:self action:@selector(viewWasTapped:)];
[[self view] addGestureRecognizer:tapRecognizer];
Run Code Online (Sandbox Code Playgroud)

然后声明你的动作选择器:

- (void)viewWasTapped:(UITapGestureRecognizer*)recognizer
{
  CGPoint location = [recognizer locationInView:[self view]];
  [UIView beginAnimations:nil context:NULL];
  [UIView setAnimationDuration:1.0f]; // take a second to animate
  [viewToMove setCenter:location];
  [UIView commitAnimations];
}
Run Code Online (Sandbox Code Playgroud)

这将使视图从一个位置动画到另一个位置.这被称为隐式动画.使用Core Animation类,例如CAKeyframeAnimation或CABasciAnimation将是显式动画.