简体   繁体   English

在动画中间为UIImageView设置动画

[英]Animate a UIImageView in the middle of an animation

I have an object that is moving right to left across the screen: 我有一个在屏幕上从右向左移动的对象:

[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:7.8];
[UIView setAnimationCurve:UIViewAnimationCurveLinear];
 myImageview.layer.position = CGPointMake(20,  myImageView.layer.position.y);
[UIView commitAnimations];

I found out that even while the animation is still happening, XCode already marks the image's location as its final destination, and in order to detect a touch on the moving image, I need to use the presentationLayer: 我发现即使动画仍在发生,XCode已经将图像的位置标记为最终目的地,为了检测运动图像上的触摸,我需要使用presentationLayer:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    [super touchesBegan:touches withEvent:event];

    UITouch *touch = [touches anyObject];
    CGPoint touchPoint = [touch locationInView:self.view];

    if ([myImageview.layer.presentationLayer hitTest:touchPoint]) {
        NSLog(@"it's a hit!");
    }
}

This part works. 这部分有效。 Now, I'd like the image to move up when it is pressed. 现在,我希望图像在按下时向上移动。 I want the image to move up while it continues it's sideways movement. 我希望图像向上移动,同时继续它的侧向移动。 Instead, this code moves the image not just up, but also all the way to its final destination on the left: 相反,此代码不仅会向上移动图像,还会一直移动到左侧的最终目标:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    [super touchesBegan:touches withEvent:event];

    UITouch *touch = [touches anyObject];   
    CGPoint touchPoint = [touch locationInView:self.view];

    if ([mouse.layer.presentationLayer hitTest:touchPoint]) {
        NSLog(@"it's a hit!");
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:0.5];
        [UIView setAnimationCurve:UIViewAnimationCurveLinear];
        myImageView.layer.position = CGPointMake( mouse.layer.position.x,  myImageView.layer.position.y - 40);
        [UIView commitAnimations];
    }
}

I want the image to move up while it continues it's sideways movement. 我希望图像向上移动,同时继续它的侧向移动。 Does anyone know of a way to accomplish this? 有谁知道这样做的方法?

Thanks so much! 非常感谢!

Have you tried setting the animation option UIViewAnimationOptionBeginFromCurrentState ? 您是否尝试过设置动画选项UIViewAnimationOptionBeginFromCurrentState

(I say option because it is an option in the block-based animation methods introduced with iOS 4. It is also available as [UIView setAnimationBeginsFromCurrentState:YES] if you can't switch away from the deprecated UIView class methods just yet.) (我说选项是因为它是iOS 4引入的基于块的动画方法中的一个选项。如果你还不能从已弃用的UIView类方法中切换出来,它也可用作[UIView setAnimationBeginsFromCurrentState:YES] 。)

touchesBegan becomes (using block animations): touchesBegan成为(使用块动画):

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    [super touchesBegan:touches withEvent:event];

    UITouch *touch = [touches anyObject];   
    CGPoint touchPoint = [touch locationInView:self.view];

    if ([mouse.layer.presentationLayer hitTest:touchPoint]) {
        NSLog(@"it's a hit!");
        [UIView animateWithDuration:0.5 delay:0.0 options:(UIViewAnimationOptionCurveLinear & UIViewAnimationOptionBeginFromCurrentState) animations:^{
            myImageView.layer.position = CGPointMake( myImageView.layer.position.x,  mouse.layer.position.y - 40);
        }completion:^(BOOL complete){
            //
        }];
    }
}

If you use that you should be able to specify the final x and y coordinates you want and have animation proceed from the point at which the object was touched to that position. 如果使用它,您应该能够指定所需的最终xy坐标,并从触摸对象的位置开始动画到该位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM