开发者

iOS/Objective-C: transitionWithView custom animation block resizes view immediately, not over duration

开发者 https://www.devze.com 2023-03-19 06:13 出处:网络
The following code is a method I created inside a UIViewController to popup/down a \"reader\" overlay on top of the controller\'s own view. The intention is for the reader to begin as transparent, siz

The following code is a method I created inside a UIViewController to popup/down a "reader" overlay on top of the controller's own view. The intention is for the reader to begin as transparent, size zero, at a specific point. "Popup" is then animated as increasing in opacity and size, and shifts towards an application frame central position. "Popdown" is subsequently animated as the reverse, shrinking back whilst moving toward a specified location, fading out.

The popup code works exactly as desired. However, the popdown version (i.e. code executed if isPopup == N开发者_运维知识库O) immediately changes the bounds rather than doing so gradually. Thus the popdown animation shows from the beginning a 1 pixel square view moving towards its destination and fading out.

-(void)popupReader:(BOOL)isPopup from:(CGPoint)loc  {
    CGFloat newAlpha = 0.0f;
    CGPoint newCenter = CGPointZero;
    CGRect newBounds = CGRectZero;
    CGRect appFrame = [UIScreen mainScreen].applicationFrame;

    CGSize readerSize = [self viewSize];
    if (isPopup) {
        newAlpha = 1.0f;
        newCenter = CGPointMake(appFrame.origin.x+appFrame.size.width/2,
                                appFrame.origin.y+appFrame.size.height/2);
        newBounds = CGRectMake(0,0,readerSize.width,readerSize.height);

        [self.view setAlpha:0.0f];
        [self.view setCenter:loc];
        [self.view setBounds:CGRectMake(0, 0, 0, 0)];
    } else {
        newCenter = loc;
        newBounds = CGRectMake(0,0,1,1);
    }

    const CGFloat animDur = 0.3f;    
    [UIView transitionWithView:self.view 
                      duration:animDur
                       options:UIViewAnimationOptionTransitionNone|UIViewAnimationOptionCurveEaseOut 
                    animations:^{
      self.view.alpha = newAlpha; 
      self.view.center = newCenter; 
      self.view.bounds = newBounds;
      } 
         completion:nil];
}

I've already tried animating just the frame, rather than bounds and center, but the result was identical.

Does anyone know why this is happening, and how I can overcome this problem?

Many thanks for your time.


from the docs:

UIViewAnimationOptionTransitionNone - An option for specifying that no transition should occur.

try one of:

UIViewAnimationOptionTransitionFlipFromLeft 
UIViewAnimationOptionTransitionFlipFromRight 
UIViewAnimationOptionTransitionCurlUp          
UIViewAnimationOptionTransitionCurlDown       

Edit: Or if you're just looking to animate those values, try

[UIView animateWithDuration:(NSTimeInterval)duration 
                 animations:(void (^)(void))animations 
                 completion:(void (^)(BOOL finished))completion];
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号