Programing

UIView 블록 기반 애니메이션을 취소하는 방법은 무엇입니까?

lottogame 2020. 10. 26. 07:39
반응형

UIView 블록 기반 애니메이션을 취소하는 방법은 무엇입니까?


나는 많은 물건과 Apple의 참고 문헌을 검색했지만 여전히 내 문제를 관리 할 수 ​​없습니다.

내가 가진 것 :

  1. 2 개의 UIImageView가있는 화면과 2 개의 UIButton이 연결된 화면
  2. 2 가지 애니메이션 :
    2.1. 첫 번째는 viewDidLoad 2.2에서 한 번만 이미지를 하나씩 확장 한 다음 축소하는 것입니다. 두 번째 버튼을 누르면 (사용자 정의 버튼, 각 UIImageView의 '내부'숨김) 적절한 UIImageView의 애니메이션이 트리거됩니다 (둘 다가 아닌 하나만) (확장, 축소).
  3. iOS4 + 용으로 글을 쓸 때 블록 기반 애니메이션을 사용하라는 지시를 받았습니다!

내가 필요한 것:

실행중인 애니메이션을 취소하는 방법은 무엇입니까? 나는 마지막 하나를 제외하고 결국 취소 할 수 있었다 ... : / 다음은 내 코드 스 니펫입니다.

[UIImageView animateWithDuration:2.0 
                               delay:0.1 
                             options:UIViewAnimationOptionAllowUserInteraction 
                          animations:^{
        isAnimating = YES;
        self.bigLetter.transform = CGAffineTransformScale(self.bigLetter.transform, 2.0, 2.0);
    } completion:^(BOOL finished){
        if(! finished) return;
        [UIImageView animateWithDuration:2.0 
                                   delay:0.0 
                                 options:UIViewAnimationOptionAllowUserInteraction 
                              animations:^{
            self.bigLetter.transform = CGAffineTransformScale(self.bigLetter.transform, 0.5, 0.5);
        } completion:^(BOOL finished){
            if(! finished) return;
            [UIImageView animateWithDuration:2.0 
                                       delay:0.0 
                                     options:UIViewAnimationOptionAllowUserInteraction 
                                  animations:^{
                self.smallLetter.transform = CGAffineTransformScale(self.smallLetter.transform, 2.0, 2.0);
            } completion:^(BOOL finished){
                if(! finished) return;
                [UIImageView animateWithDuration:2.0 
                                           delay:0.0 
                                         options:UIViewAnimationOptionAllowUserInteraction 
                                      animations:^{
                    self.smallLetter.transform = CGAffineTransformScale(self.smallLetter.transform, 0.5, 0.5);
                }
                                      completion:^(BOOL finished){
                                          if (!finished) return;
                                          //block letter buttons
                                          [self.bigLetterButton setUserInteractionEnabled:YES];
                                          [self.smallLetterButton setUserInteractionEnabled:YES];
                                          //NSLog(@"vieDidLoad animations finished");
                                      }];
            }];
        }];
    }];

어떻게 든 smallLetter UIImageView가 제대로 작동하지 않습니다. (버튼을 통해) 눌 렸을 때 bigLetter가 애니메이션을 제대로 취소하고 있기 때문입니다 ... 미리 감사드립니다 :)

편집 : 나는이 솔루션을 사용했지만 smallLetter UIImageView를 축소하는 데 여전히 문제가 있습니다-전혀 취소하지 않습니다 ... 솔루션

EDIT2 : 다음 / 이전 방법의 시작 부분에 이것을 추가했습니다.

- (void)stopAnimation:(UIImageView*)source {
    [UIView animateWithDuration:0.01
                          delay:0.0 
                        options:(UIViewAnimationOptionBeginFromCurrentState | UIViewAnimationOptionAllowUserInteraction)
                     animations:^ {
                         source.transform = CGAffineTransformIdentity;
                     }
                     completion:NULL
     ];
}

문제가 계속됩니다 ... : / 애니메이션 체인의 문자에 대한 마지막 애니메이션을 중단하는 방법을 모릅니다


다음을 호출하여 뷰의 모든 애니메이션을 중지 할 수 있습니다.

[view.layer removeAllAnimations];

(view.layer에서 메소드를 호출하려면 QuartzCore 프레임 워크를 가져와야합니다.)

If you want to stop a specific animation, not all animations, your best best bet is to use CAAnimations explicitly rather than the UIView animation helper methods, then you will have more granular control and can stop animations explicitly by name.

The Apple Core Animation documentation can be found here:

https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreAnimation_guide/CreatingBasicAnimations/CreatingBasicAnimations.html


For iOS 10 use UIViewPropertyAnimator to animate. It provides methods to start, stop and pause UIView animations.

 let animator = UIViewPropertyAnimator(duration: 2.0, curve: .easeOut){
        self.view.alpha = 0.0
 }
 // Call this to start animation.
 animator.startAnimation()

 // Call this to stop animation.
 animator.stopAnimation(true)

I'd add to Nick's answer that to make removeAllAnimations smooth next idea be very handy.

[view.layer removeAllAnimations];
[UIView transitionWithView:self.redView
                  duration:1.0f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
                      [view.layer displayIfNeeded];
                  } completion:nil];

You can try this (in Swift):

UIView.setAnimationsEnabled(false)
UIView.setAnimationsEnabled(true)

Note: you can put code between those two calls if necessary, for example:

UIView.setAnimationsEnabled(false)
aview.layer.removeAllAnimations() // remove layer based animations e.g. aview.layer.opacity
UIView.setAnimationsEnabled(true)

참고URL : https://stackoverflow.com/questions/9569943/how-to-cancel-uiview-block-based-animation

반응형