0

メインビューでUIButtonを拡大縮小しようとしています。最初にアクションボタンを押すと、ボタンがズームインし、もう一度押すとズームアウトしますが、もう一度押すとズームインします。何も起こりません。 ..ここに私のコードがあります:

ズームインおよびズームアウトする方法は、UIVIEWのObjective-Cカテゴリにあります

- (void)viewDidLoad
[super viewDidLoad];

//this button is being added in the storyboard
[self.viewToZoom removeFromSuperview];

}

- (IBAction)zoomButton:(id)sender {

if (isShown) {
    [self.view removeSubviewWithZoomOutAnimation:self.viewToZoom duration:1.0 option:0];
    isShown = NO;
} else {
    [self.view addSubviewWithZoomInAnimation:self.viewToZoom duration:1.0 option:0];
    isShown = YES;
}

}

UIView+Animation.m


- (void) addSubviewWithZoomInAnimation:(UIView*)view duration:(float)secs option:(UIViewAnimationOptions)option {

CGAffineTransform trans = CGAffineTransformScale(view.transform, 0.01, 0.01);

view.transform = trans; // do it instantly, no animation
[self addSubview:view];
// now return the view to normal dimension, animating this tranformation
[UIView animateWithDuration:secs delay:0.0 options:option
                 animations:^{
                     view.transform = CGAffineTransformScale(view.transform, 100.0, 100.0);
                 }
                 completion:^(BOOL finished) {
                     NSLog(@"done");
                 } ];   
}


- (void) removeSubviewWithZoomOutAnimation:(UIView*)view duration:(float)secs option:(UIViewAnimationOptions)option {

// now return the view to normal dimension, animating this tranformation
[UIView animateWithDuration:secs delay:0.0 options:option
                 animations:^{
                     view.transform = CGAffineTransformScale(view.transform, 0.01, 0.01);

                 }
                 completion:^(BOOL finished) {
                     [view removeFromSuperview];
                 }];    
}

ありがとう、ニュートン

4

1 に答える 1

4

ニュートンは、removeSubviewWithZoomOutAnimation終了するview.transformと、ビューの元のサイズを0.01に縮小したアフィン変換です。問題はaddSubviewWithZoomInAnimation、2回目に電話をかけると、再び0.01だけview.transformスケールダウンしているのに、0.0001にスケールダウンされることです。これは、探しているものではありません。

view.transform = CGAffineTransformIdentity;次のように、両方のアニメーションの先頭に追加するだけです。

- (void) addSubviewWithZoomInAnimation:(UIView*)view duration:(float)secs option:(UIViewAnimationOptions)option {
    view.transform = CGAffineTransformIdentity; 
    CGAffineTransform trans = CGAffineTransformScale(view.transform, 0.01, 0.01);

    view.transform = trans; // do it instantly, no animation
    [self addSubview:view];
    // now return the view to normal dimension, animating this tranformation
    [UIView animateWithDuration:secs delay:0.0 options:option
                     animations:^{
                         view.transform = CGAffineTransformScale(view.transform, 100.0, 100.0);
                     }
                     completion:^(BOOL finished) {
                         NSLog(@"done");
                     } ];   
}


- (void) removeSubviewWithZoomOutAnimation:(UIView*)view duration:(float)secs option:(UIViewAnimationOptions)option {
    view.transform = CGAffineTransformIdentity;
    // now return the view to normal dimension, animating this tranformation
    [UIView animateWithDuration:secs delay:0.0 options:option
                     animations:^{

                         view.transform = CGAffineTransformScale(view.transform, 0.01, 0.01);

                     }
                     completion:^(BOOL finished) {

                         [view removeFromSuperview];
                     }];    
}

UIViewAnimationOptionBeginFromCurrentStateまた、 UIViewAnimationOptionsを渡すことをお勧めします。これにより、すばやくズームインおよびズームアウトしたときのアニメーション結果が向上します。

お役に立てれば!

于 2012-05-04T09:31:58.527 に答える