3

我有一个UIImageView应该从size (0,0) --> (93,75)

我有以下

  [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而是从左上角动画。

有人有线索吗?

4

4 回答 4

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");
                     }];
于 2013-06-28T12:00:08.713 回答
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"];
于 2013-06-28T11:14:03.753 回答
0

我的建议可能是一种 hack,但这将是实现您想要的更简单的方法:

  • 您将 imageView 的初始帧设置为:

    CGRectMake(center, center, 0, 0); 
    

    因此,在您的情况下,它将更接近:

    CGRectMake(46, 37, 0, 0);
    
  • 然后用类似这样的新矩形对其进行动画处理:

    CGRectMake(0, 0,93,75); 
    

    您可以为此使用简单的 UIViewAnimation ;

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

希望它有效

于 2013-06-28T10:22:26.610 回答
0

您要实现的是比例转换,即将 UIView 大小从 size(0,0) 设置为 size(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
 ];

此动画将根据您的需要围绕视图的中心点发生,从而避免使用anchorPointand frame,这可能很棘手。阅读有关此答案之间的关系frame的更多信息。anchorPoint

于 2013-06-28T10:34:59.407 回答