如何在 UIView 上执行转换和框架更改

How to perform a Transformation and Frame Change on a UIView

我正在尝试创建一个动画,使视图看起来像弹出给您一样。 'Animation Option 1' 正是我想要的效果,但在框架更改之前执行转换会弄乱框架(在 view.transform=CGAffineTransformIdentity; 之后,视图框架与原始大小不匹配)。 'Animation Option 2' 是个好主意,但看起来很糟糕。知道如何进行转换和更改视图框架吗?

//ANIMATION OPTION 1
CGRect frame=view.frame;
view.transform=CGAffineTransformMakeScale(0.94f, 0.94f);

frame.origin.y=10.0f; //The view is originally off screen, this is to show
[UIView animateWithDuration:0.2f animations:^{
    [view setFrame:frame];
}completion:^(BOOL finished){
    [UIView animateWithDuration:0.2f animations:^{
        view.transform=CGAffineTransformIdentity;//This pops the view up
    }];
}];



//ANIMATION OPTION 2
CGRect frame=view.frame;
frame.size.width=296;
frame.size.height=296;
frame.origin.x=8;
frame.origin.y=8;

[UIView animateWithDuration:0.2f animations:^{
    [view setFrame:frame];
}completion:^(BOOL finished){
     CGRect frame=view.frame;
     frame.size.width=300;
     frame.size.height=300;
     frame.origin.x=10;
     frame.origin.y=10;
    [UIView animateWithDuration:0.2f animations:^{
        [view setFrame:frame];
    }];
}];

您在变换后更改框架,因此标识不适用于变换。不要更改 x 和 y,而是在 UIView 上使用翻译。我的 objective c 生锈了,但看看这是否是您的想法。我延迟它以便您可以看到开始位置和结束位置 position.Also 使用翻译而不是操纵框架的优点是如果您使用 AutoLayout 就不会弄乱框架。

#import "ViewController.h"

@interface ViewController ()
    @property UIView *testView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _testView =  [[UIView alloc]init];
    [_testView setFrame:CGRectMake(20, 100, self.view.bounds.size.width - 40, 100)];
    [_testView setBackgroundColor:[UIColor greenColor]];
    [self.view addSubview:_testView];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    //just delaying it so you can see the start position
    double delayInSeconds = 2.0;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds * NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        //code to be executed on the main queue after delay
        self.testView.transform = CGAffineTransformConcat(CGAffineTransformMakeScale(0.94, 0.94), CGAffineTransformMakeTranslation(0, 10));
        self.testView.alpha = 0;
        [UIView animateWithDuration:1 delay:2 options:(UIViewAnimationOptionCurveEaseInOut) animations:^{
            self.testView.transform=CGAffineTransformIdentity;//This pops the view up
            self.testView.alpha = 1;

        } completion:^(BOOL finished) {

        }];

    });
}


@end

感谢@agibson007 这是我一直在寻找的最终效果:

[view setFrame:CGRectMake(8, 10, WIDTH-16, height)];
view.transform = CGAffineTransformConcat(CGAffineTransformMakeScale(0.94, 0.94), CGAffineTransformMakeTranslation(0, -height));


[UIView animateWithDuration:0.2f delay:0 options:(UIViewAnimationOptionCurveEaseInOut) animations:^{
    view.transform = CGAffineTransformConcat(CGAffineTransformMakeScale(0.94, 0.94), CGAffineTransformMakeTranslation(0, 0));
}completion:^(BOOL finished) {
    [UIView animateWithDuration:0.2f delay:0 options:(UIViewAnimationOptionCurveEaseInOut) animations:^{
        view.transform=CGAffineTransformIdentity;
    }];
}];