2013-06-28 68 views
3

中心一个UIImageView我有一个UIImageView应从size (0,0) --> (93,75)动画从UIImageView的

动画我已经

[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionTransitionNone 
       animations:^{ 
        imgButton.layer.anchorPoint = CGPointMake(imgButton.bounds.size.width/2, imgButton.bounds.size.height/2); 
        CGRect btnFrame = imgButton.frame; 
        btnFrame.size.width = 105; 
        btnFrame.size.height = 85; 
        imgButton.frame = btnFrame; 
       } 
       completion:^(BOOL finished) { 
        [self animageButton2:imgButton]; 
       }]; 

这是工作,但我唯一的问题是,它是不是动画以下从UIImageView的中心,但从左上角。

有人有线索吗?

+0

您是否真的阅读过'anchorPoint'的文档?我猜不是,因为你没有给它正确的价值。此外,它仅适用于'CGAffineTransform'操作,而不是大小。如果你想这样做,你将不得不自己调整原点。我建议将刻度设置为零并将其设置为1. – borrrden

回答

0

我的建议可能是种技巧,但它会达到你想要的东西更简单的方法:

  • 您设置的ImageView的初步框架为:

    CGRectMake(center, center, 0, 0); 
    

    所以在你的情况它会更接近:

    CGRectMake(46, 37, 0, 0); 
    
  • 然后用somethi的新rect动画它ng像这样:

    CGRectMake(0, 0,93,75); 
    

    你可以使用简单的UIViewAnimation这个;

    [UIView beginAnimations:@"zoom" context:NULL]; 
        [UIView setAnimationDuration:0.3]; 
        imageView.frame = CGRectMake(0, 0, 93, 75); ; 
        [UIView commitAnimations]; 
    

希望工程

0

你所想要实现一个尺度变换,即从动画尺寸(0,0)的UIView大小尺寸(W,H)。 CGAffineTransformMakeScale可以达到同样的效果。

最初,当您创建视图应用CGAffineTransformMakeScale(0.0,0.0)时,动画大小缩放至CGAffineTransformMakeScale(1.0,1.0)

代码:

// Create the view and apply correct frame 
UIView *tagView = [[UIView alloc] initWithFrame:CGRectMake(50, 100, 100, 100)]; 
tagView.tag = kTag; 
//Apply scale transform, to make size(0,0) 
tagView.transform = CGAffineTransformMakeScale(0,0); 
[self.view addSubview:tagView]; 

/// ............ 

UIView *tagView = [self.view viewWithTag:kTag]; 
//Now animate the view size scale up 
[UIView animateWithDuration:0.5 
     delay:0 
     options:UIViewAnimationOptionCurveEaseOut 
     animations:^{ 
         tagView.transform = CGAffineTransformMakeScale(1.0,1.0); 
        } 
     completion:NULL 
]; 

这个动画会发生有关视图的中心点,只要你想,这样就避免了需要用anchorPointframe发挥,这可能会非常棘手。请从this answer了解更多关于frameanchorPoint之间的关系。

2

只是试试这个。进口#import <QuartzCore/QuartzCore.h>然后把下面的代码同时加入了uimageView到self.view的子视图

CABasicAnimation *zoomAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"]; 
[zoomAnimation setDuration:0.8]; 
[zoomAnimation setRepeatCount:1]; 
[zoomAnimation setFromValue:[NSNumber numberWithFloat:0.1]]; 
[zoomAnimation setToValue:[NSNumber numberWithFloat:1.0]]; 
[zoomAnimation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]]; 
[[imageView layer] addAnimation:zoomAnimation forKey:@"zoom"]; 
+0

喂。我想在动画结尾添加一些动作,怎么做? – kemdo

4

我和Borrden的帮助下解决了这个问题。这就是我的解决方案。

首先创建一个ImageView的

UIImageView *imgLineup = [[UIImageView alloc]initWithFrame:CGRectMake(10, y, 93, 75)]; 
imgLineup.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.0, 0.0); 

然后到下面。

[UIView animateWithDuration:0.5 delay:0 options:UIViewAnimationOptionCurveEaseInOut 
        animations:^{ 
         imgButton.layer.anchorPoint = CGPointMake(0.5, 0.5); 
         imgButton.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.0, 1.0); 

        } 
        completion:^(BOOL finished) { 
         NSLog(@"done"); 
        }]; 
+3

'anchorPoint'的默认值是'(0.5,0.5)',因此重新分配它是多余的。 – Amar