UIButton滑入,而不是淡入

时间:2013-07-24 00:27:11

标签: ios uibutton uiviewanimation

我在UIButton中有一个UIViewController我目前正在“淡入”以查看应用何时打开。 我希望让UIButton从左向右滑入。

我无法弄清楚如何让它从左向右滑入,我的尝试失败了,尽管我已经将“淡入”的东西压得很稳固。

你可以给我任何帮助吗?谢谢!我可以根据需要添加更多代码 -

ViewController.h

@property (weak, nonatomic) IBOutlet UIButton *buttonOne;

ViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
        NSTimer *timermovebutton = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(movebutton) userInfo:nil repeats:NO];
    [timermovebutton fire];
}

-(void) movebuttontwo
{
    buttonTwo.alpha = 0;
    [UIView beginAnimations:Nil context:Nil];
    [UIView setAnimationDelay:0.5];
    [UIView setAnimationCurve:UIViewAnimationTransitionCurlUp];

    [UIView setAnimationDelegate:self];
    [UIView setAnimationDuration:1.0];
    buttonTwo.alpha = 1.0;

    [UIView setAnimationDidStopSelector:@selector(animationDidStop:finished:context:)];
    [UIView commitAnimations];
}

2 个答案:

答案 0 :(得分:2)

您需要为按钮框架设置动画,而不是使用动画曲线。

从iOS 4开始,我们已经有了UIView动画块:

// first set the UIButton frame to be outside of your view:
// we are only concerned about it's location on the X-axis so let's keep all properties the same
[buttonTwo setFrame:CGRectMake(CGRectGetMaxX(self.view.frame), CGRectGetMinY(buttonTwo.frame), CGRectGetWidth(buttonTwo.frame), CGRectGetHeight(buttonTwo.frame))];
[UIView animateWithDuration:0.5
                      delay:0
                    options:UIViewAnimationCurveEaseOut
                 animations:^{
                     // set the new frame
                     [buttonTwo setFrame:CGRectMake(0, CGRectGetMinY(buttonTwo.frame), CGRectGetWidth(buttonTwo.frame), CGRectGetHeight(buttonTwo.frame))];
                 } 
                 completion:^(BOOL finished){
                     NSLog(@"Done!");
                 }
];

您可以查看good tutorial over at Ray Wenderlich了解详情。

答案 1 :(得分:1)

//Set your button off the screen to the left
yourButton.frame = CGRectMake(-yourButton.frame.size.width, yourButton.frame.origin.y, CGRectGetWidth(yourButton.frame), CGRectGetHeight(yourButton.frame));

//Create the ending frame or where you want it to end up on screen
CGRect newFrm = yourButton.frame;
newFrm.origin.x = 100;

//Animate it in
[UIView animateWithDuration:2.0f animations:^{
    yourButton.frame = newFrm;
}];