2012-05-04 21 views
0

我試圖在主視圖中縮放UIButton,當我首先按動按鈕時,按鈕放大,當我再按一次它縮小,但是當我再次按下它時放大..什麼也沒有發生。這裏是我的代碼:CGAffineTransformScale a UIButton

方法進行放大和縮小是在一個Objective-C的類別在UIView的

- (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

牛頓,當removeSubviewWithZoomOutAnimation結束時view.transform是一個仿射變換,將視圖的原始尺寸縮小到0.01。問題是,當您第二次再次調用addSubviewWithZoomInAnimation時,您再次縮小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提高了動畫結果時快速放大和縮小。

希望這會有所幫助!

+0

夥計,真棒..我沒有看到..!我會在家裏測試它,並給你你的大拇指......;)謝謝。 –

+0

是的..它能正常工作..非常感謝。 –