我正在尝试使用在屏幕上浮动的块(UIViews)创建一个iOS应用程序。我让它们漂浮在屏幕上,但我也希望用户能够在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];
}
答案 0 :(得分:2)
我有一个演示项目,向您展示如何使用“S”曲线在屏幕上制作“船”动画,如下所示:
我使用的解决方案是制作关键帧动画(实际上它是构成分组动画一部分的关键帧动画,但您可能不需要其余的分组动画:它是关键帧使弯曲的路径形状的动画)。也许你可以调整我正在做的事情,根据你自己的目的修改它?
该代码可从此处下载:
https://github.com/mattneub/Programming-iOS-Book-Examples/tree/master/ch17p501groupedAnimation
我的书中详细讨论过。关键帧动画一般:
http://www.apeth.com/iOSBook/ch17.html#_keyframe_animation
这个特例: