Iphone iOS平移和缩放动画

Iphone iOS平移和缩放动画,iphone,ios,objective-c,cgaffinetransform,uianimation,Iphone,Ios,Objective C,Cgaffinetransform,Uianimation,我正在制作UIButton动画,其中: ui按钮设置在屏幕的底部中央,并按比例缩小 _menuBtn.transform = CGAffineTransformMakeScale(0.1f, 0.1f); 当应用程序启动时,它应该移动到屏幕的左下角,因为它的规模或增长到原来的大小 - (void)viewDidLoad { [super viewDidLoad]; _menuBtn.frame = CGRectMake(160, 513, 30, 30); _men

我正在制作UIButton动画,其中:

ui按钮
设置在屏幕的底部中央,并按比例缩小

_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>>
NSLog(@“%@”,\u myBtn);
2013-08-14 09:22:38.913 GJCoolNavi[339:c07]
这是在制作动画之前…动画之后的结果是:

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>>
2013-08-1409:30:25.719 GJCoolNavi[612:c07]

你为什么不这样做

_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];
}

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

这个现象表明按钮的原始帧是错误的,可能是因为它自动调整了大小。在开始动画之前,请尝试将其帧设置为底部中心

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    _menuBtn.superview.frame = CGRectMake(0, 513, 320, 30); // This is a quick and dirty solution to make sure your button's superview is in the right place. You probably don't want to do this and are more likely to review your view hierarchy.
    _menuBtn.frame = CGRectMake(145, 0, 30, 30); // Set the frame to the bottom center, please ensure that _menuBtn's transform hasn't been changed before this step
    _menuBtn.transform = CGAffineTransformMakeScale(0.1f, 0.1f);
    [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];
}

我已经使用了你的代码,它从下中移动到左下角非常完美。可能是您的superview没有正确的矩形。请检查。

这解决了类似的问题

应用(UIButton).imageView动画

[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.imageView.transform = CGAffineTransformConcat(scaleTrans, lefttorightTrans);
[UIView commitAnimations];

我试过这个代码,但它的作用是从屏幕的左上角开始,一直到屏幕的底部中心。我的代码所要做的是,它应该从屏幕的底部中心向左下方移动。然后把它放在屏幕的底部中间,如果你使用得当,什么都不做。但它会让人困惑(举个例子),特别是当你问按钮在哪里时。事实上请在动画之前打印按钮的框架。我没有理解你。打印按钮的框架是什么意思?对不起,我是新手。就是这样。这是由于使用自动布局…非常感谢你的帮助。福格迈斯特我按照你的建议,通过将其帧设置为底部中心..但仍然在应用CGAffineTransformMakeTransform时,按钮仍然从右下方开始动画,而不是从底部中心开始动画..我能想到的原因可能是按钮的superview也位于错误的位置。请在动画块之前检查按钮及其superview的帧。您可以在
\u menuBtn.transform=CGAffineTransformMakeScale(0.1f,0.1f)下方打印日志,例如:
NSLog(@“\u menuBtn:%@;\u menuBtn.superview:%@“,\u menuBtn,\u menuBtn.superview”)我设置了按钮的superview,但仍然没有效果..设置superview后,以下是日志显示的内容:
2013-08-14 15:16:52.686 GJCoolNavi[12708:c07]\u menuBtn:_menuBtn.superview:
我已经在动画开始之前打印了日志。啊,这就是问题所在!按钮超级视图的
原点是{160513},这可能是屏幕的底部中心位置。但是您的按钮的
原点是{158.5511.5},这是相对于其超级视图的位置,换句话说,它位于屏幕的右下角。请尝试以下操作:
\u menuBtn.superview.frame=CGRectMake(0,513,320,30)_menuBtn.frame=CGRectMake(145,0,30,30)在动画块之前。我会更新我的答案。使用你建议的代码,菜单框是
(158.5-6.5;3 3)
,超级视图是
(0 513;320 30)
。按钮仍然从右下角开始动画。我的第一个想法可能与主题无关,但为什么你没有像苹果在iOS4或更高版本上推荐的那样使用基于块的动画呢。在我的回答中,显然它仍然不起作用。仍然在努力弄清楚发生了什么以及为什么。谢谢!!!这是唯一一个对我有效的解决方案,而且它工作得完美无缺!!!