簡體   English   中英

在動畫中間為UIImageView設置動畫

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

我有一個在屏幕上從右向左移動的對象:

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

我發現即使動畫仍在發生,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!");
    }
}

這部分有效。 現在,我希望圖像在按下時向上移動。 我希望圖像向上移動,同時繼續它的側向移動。 相反,此代碼不僅會向上移動圖像,還會一直移動到左側的最終目標:

- (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];
    }
}

我希望圖像向上移動,同時繼續它的側向移動。 有誰知道這樣做的方法?

非常感謝!

您是否嘗試過設置動畫選項UIViewAnimationOptionBeginFromCurrentState

(我說選項是因為它是iOS 4引入的基於塊的動畫方法中的一個選項。如果你還不能從已棄用的UIView類方法中切換出來,它也可用作[UIView setAnimationBeginsFromCurrentState:YES] 。)

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){
            //
        }];
    }
}

如果使用它,您應該能夠指定所需的最終xy坐標,並從觸摸對象的位置開始動畫到該位置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM