문제

화면을 가로질러 오른쪽에서 왼쪽으로 움직이는 개체가 있습니다.

[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 setAnimationBeginsFromCurrentState:YES] 아직 더 이상 사용되지 않는 UIView 클래스 메서드에서 전환할 수 없는 경우.)

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

그것을 사용한다면 최종을 지정할 수 있어야합니다 x 그리고 y 원하는 좌표로 이동하고, 객체를 터치한 지점부터 해당 위치까지 애니메이션이 진행되도록 합니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top