我有一个UIView,我想动画到用户手指当前的位置。
因此,当触摸开始时,它必须设置为点按位置的动画。
触摸移动时,必须将目的地调整到新位置。
触摸结束时,必须将动画结束到最后一个点击位置。
我尝试了几件事但没有成功。我尝试在touchesMoved
中使用beginFromCurrentState创建一个新动画,但这也不起作用。
答案 0 :(得分: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];
然后声明你的动作选择器:
- (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];
}
这会将视图从一个位置设置为另一个位置。这被称为隐式动画。使用Core Animation类,例如CAKeyframeAnimation或CABasciAnimation将是显式动画。
答案 1 :(得分:0)
需要更多详细信息..但您确定从相应的视图中使用touchesBegan
,touchesMoved
和touchesEnded
(即不是您要移动的视图) ?还要确保userInteractionEnabled = YES
。尝试记录来自touchesBegan
的触摸,以确保您接收到触摸。
或者您可能只想在https://developer.apple.com/library/ios/#samplecode/MoveMe/Introduction/Intro.html查看Apples MoveMe
示例代码,他们已经为您解决了所有这些问题。