iphone - iOS平移和缩放动画

标签 iphone ios objective-c cgaffinetransform uianimation

我正在研究 UIButton 动画,其中:

UIButton 被设置在屏幕的底部中心,并缩放到一个小尺寸

_menuBtn.transform = CGAffineTransformMakeScale(0.1f, 0.1f);

当应用程序启动时,它应该会移动到屏幕的左下角,因为它会缩放或增长到其原始大小。

- (void)viewDidLoad
{
    [super viewDidLoad];

    _menuBtn.frame = CGRectMake(160, 513, 30, 30); 
    _menuBtn.superview.frame = CGRectMake(160, 513, 30, 30);

    _menuBtn.transform = CGAffineTransformMakeScale(0.1f, 0.1f);
    NSLog(@"_menuBtn: %@ ; _menuBtn.superview: %@", _menuBtn, _menuBtn.superview);
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:5];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut];
    CGAffineTransform scaleTrans  = CGAffineTransformMakeScale(1.0f, 1.0f);
    CGAffineTransform lefttorightTrans  = CGAffineTransformMakeTranslation(-200.0f,0.0f);
    _menuBtn.transform = CGAffineTransformConcat(scaleTrans, lefttorightTrans);
    [UIView commitAnimations];

}

问题

当动画开始时,按钮开始从屏幕的右下角开始移动,而不是在它应该在的底部中心位置。有什么帮助吗?

记录结果

NSLog(@"%@", _myBtn);

2013-08-14 09:22:38.913 GJCoolNavi[339:c07] <UIButton: 0x813ea30; frame = (0 0; 0 0); opaque = NO; autoresize = TM+BM; layer = <CALayer: 0x813eaf0>>

那是在做动画之前...动画之后的结果是:

2013-08-14 09:30:25.719 GJCoolNavi[612:c07] <UIButton: 0x71206d0; frame = (160 294; 0 0); opaque = NO; autoresize = TM+BM; animations = { transform=<CABasicAnimation: 0x7536a80>; position=<CABasicAnimation: 0x7537dd0>; }; layer = <CALayer: 0x7120790>>

最佳答案

你为什么不这样做......

_menuBtn.transform = CGAffineTransformMakeScale(0.1, 0.1);

[UIView animateWithDuration:5.0
options:UIViewAnimationOptionCurveEaseOut
animations:^(){
    _menuBtn.transform = CGAffineTransformMakeScale(1.0, 1.0);
    _menuBtn.center = self.view.center;
}
completion:nil];

我会避免使用转换来移动东西。改为更改框架。

编辑

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];

    // for convenience I'm pulling these values out in to variables.
    float buttonWidth = _menuBtn.frame.size.width;
    float buttonHeight = _menuBtn.frame.size.height;
    float viewWidth = self.view.frame.size.width;
    float viewHeight = self.view.frame.size.height;

    // set the button frame to be the bottom center
    // note you shouldn't have to do this as Interface Builder should already place it there.
    // place the button in the horizontal center and 20 points from the bottom of the view.
    _menuBtn.frame = CGRectMake((viewWidth - buttonWidth) * 0.5, viewHeight - buttonHeight - 20, buttonWidth, buttonHeight);

    // scale the button down before the animation...
    _menuBtn.transform = CGAffineTransformMakeScale(0.1, 0.1);

    // now animate the view...
    [UIView animateWithDuration:5.0
                          delay:0.0
                        options:UIViewAnimationOptionCurveEaseOut
                     animations:^{
                         _menuBtn.transform = CGAffineTransformIdentity;
                         _menuBtn.frame = CGRectMake(viewWidth - buttonWidth - 20, viewHeight - buttonHeight - 20, buttonWidth, buttonHeight);
                     }
                     completion:nil];
}

试试这个,让我知道会发生什么。

关于iphone - iOS平移和缩放动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18206430/

相关文章:

iphone - 如何调试 if(sqlite3_step(selectstmt) == SQLITE_DONE) 的结果

iphone - 如何将静态广告横幅添加到 UITableViewController 中?

iphone - 如何更改 UITabBarController 更多按钮的标题?

iphone - 如何删除 UITextView 中的所有按钮?

ios - UIAlertView 取消按钮使应用程序崩溃

ios - UIStepper:如何知道用户点击了步进器的哪个按钮(减号或加号按钮)

ios - 计算自上次发布以来的天数?

ios - 在 IOS 中获取错误的运营商名称

ios - Parse.com 查询限制 - 影响 whereKey 限制?

ios - AFNetworking - 重试直到 API 正确响应