programing

UIView 블록 기반 애니메이션을 취소하려면 어떻게 해야 합니까?

telebox 2023. 8. 25. 23:31
반응형

UIView 블록 기반 애니메이션을 취소하려면 어떻게 해야 합니까?

저는 많은 SO 자료와 Apple의 레퍼런스를 검색했지만 여전히 제 문제를 관리할 수 없습니다.

내가 가진 것:

  1. 2가 있는 화면UIImageViews와 2UIButton그들과 연결된 s.
  2. 애니메이션 2종:
    1. 각 이미지의 스케일업 및 스케일다운(한 번씩)viewDidLoad
    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 프레임워크를 가져와야 합니다.)

모든 애니메이션이 아닌 특정 애니메이션을 중지하려면 UIView 애니메이션 도우미 방법 대신 CAA 애니메이션을 명시적으로 사용하는 것이 가장 좋습니다. 그러면 보다 세밀한 제어가 가능하고 이름으로 애니메이션을 명시적으로 중지할 수 있습니다.

Apple Core 애니메이션 설명서는 다음 사이트에서 찾을 수 있습니다.

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

iOS 10의 경우 UIViewPropertyAnimator를 사용하여 애니메이션을 만듭니다.UIView 애니메이션을 시작, 중지 및 일시 중지하는 방법을 제공합니다.

 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)

모든 애니메이션을 원활하게 제거하려면 다음 아이디어가 매우 유용하다는 Nick의 답변에 추가하고 싶습니다.

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

다음을 시도할 수 있습니다(Swift에서).

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

참고: 필요한 경우 두 통화 사이에 코드를 넣을 수 있습니다. 예를 들어 다음과 같습니다.

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

반응형