【发布时间】:2013-04-14 16:29:49
【问题描述】:
我正在尝试创建一个带有块在屏幕上浮动的 iOS 应用程序(UIViews)。我让它们漂浮在屏幕上,但我也希望用户能够在它们下落时在 x 轴上移动它们。我试图用下面的代码来做,但它们只是掉下来,不会从左向右移动。我的问题是我试图用手指从左到右移动它,因为它已经在屏幕上移动了。我怎样才能调整下面的代码来工作?
注意:我可以将视图从左向右移动,而不会在屏幕上向下移动,我也可以在屏幕上向下移动,而无需从左向右移动。当我将两者结合起来时就会出现问题。
Y 轴动画
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:letView.speed];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
letView.layer.frame = CGRectMake(letView.layer.frame.origin.x, [[UIScreen mainScreen] bounds].size.height, letView.layer.frame.size.width, letView.layer.frame.size.height);
[UIView commitAnimations];
触摸动画
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
//Goes through an array of views to see which one to move
for (LetterView * view in _viewArray) {
if (CGRectContainsPoint(view.frame, touchLocation)) {
dragging = YES;
currentDragView = view;
[currentDragView.layer removeAllAnimations];
}
}
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [[event allTouches] anyObject];
CGPoint touchLocation = [touch locationInView:self.view];
if (dragging) {
CGPoint location = touchLocation;
currentDragView.center = CGPointMake(location.x, currentDragView.center.y);
}
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
dragging = NO;
[self checkForCorrectWord];
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:currentDragView.speed];
[UIView setAnimationDelay:0.0];
[UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
currentDragView
.layer.frame = CGRectMake(currentDragView.layer.frame.origin.x, [[UIScreen mainScreen] bounds].size.height, currentDragView.layer.frame.size.width, currentDragView.layer.frame.size.height);
[UIView commitAnimations];
}
【问题讨论】:
-
您是否进行了调试以检查您的“CGRectContainsPoint”是否匹配? _viewArray 中的视图都是 self.view 的直接子类吗?
-
我能够将视图从左向右移动,而不会在屏幕上向下移动,并且我能够在不移动它们的情况下将它们向下移动。当我将两者结合起来时就会出现问题。
-
屏幕下方的动画效果如何?您的触摸处理代码正在从触摸视图层中删除所有动画。您的问题只提供了一半信息的详细信息。
-
如果可能的话,我不想停止动画在屏幕上向下移动。
-
接下来,您尝试将它们结合起来做什么?停止并盯着一个新的动画?离开现有的动画?还有什么?
标签: ios uiview quartz-graphics