简体   繁体   English

动画:如何在加载视图时使 UIbutton 从右向左移动?

[英]Animation : How to make UIbutton move from right to left when view is loaded?

I have not done any animation stuff with iphone development so far.到目前为止,我还没有用 iphone 开发任何动画东西。

Can anybody help me how to make UIbutton move from right to left when view is loaded?任何人都可以帮助我如何在加载视图时使 UIbutton 从右向左移动?

I want to generate effect like that : when view is loaded, the button appears moving from right end to left & stops at its place.我想产生这样的效果:当视图加载时,按钮出现从右端向左移动并停在它的位置。 Is it possible?是否有可能?

Any help regarding this animation stuff is appreciated.感谢有关此动画内容的任何帮助。 Please help with some sample if available.如果可用,请帮助提供一些样本。

Thanks in advance.提前致谢。

Just to make it even slicker, I would suggest not changing the UIView's frame but its center;只是为了让它更光滑,我建议不要改变 UIView 的框架,而是它的中心;

CGPoint newLeftCenter = CGPointMake( 20.0f + myButton.frame.size.width / 2.0f, myButton.center.y);
[UIView beginAnimations:nil context:nil]; 
[UIView setAnimationDuration:1.0f];
myButton.center = newLeftCenter;
[UIView commitAnimations];

in your viewDidAppear:(BOOL)animated method you can change frame of your button inside animation block.在您的 viewDidAppear:(BOOL)animated 方法中,您可以更改动画块内按钮的框架。 so you'll see what you want.所以你会看到你想要的。 Check documentation about animations in UIView class reference.查看 UIView 类参考中有关动画的文档。 The simpliest animation block will be like that(sorry, i have no mac nearby right now, so code may not be working just after copy and paste)最简单的动画块就是这样(抱歉,我现在附近没有 mac,所以代码在复制和粘贴后可能无法工作)

 [UIView beginAnimations:nil context:nil]; [UIView setAnimationDuration:1.0f]; [myButton setFrame:newFrame]; [UIView commitAnimations];

From top to bottom animation从上到下动画

CATransition *animation = [CATransition animation];
[animation setDuration:0.4];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromBottom];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[TopButton layer] addAnimation:animation forKey:@"SwitchToDown"];

/* Common transition subtypes. /* 常见的转换子类型。 */ */

kCATransitionFromRight kCATransitionFromRight

kCATransitionFromLeft kCATransitionFromLeft

kCATransitionFromTop kCATransitionFromTop

kCATransitionFromBottom kCATransitionFromBottom

Swift:迅速:

    let transition1: CATransition = CATransition()
    let timeFunc1 : CAMediaTimingFunction = CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut)
    transition1.duration = 1.0
    transition1.timingFunction = timeFunc1
    transition1.type = kCATransitionPush
    transition1.subtype = kCATransitionFromRight
    self.yourBtnORViewRef.layer.addAnimation(transition1, forKey: kCATransition)

Objective-C目标-C

CATransition *animation = [CATransition animation];
[animation setDuration:1.0];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromRight];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[self.yourBtnORViewRef layer] addAnimation:animation forKey:@"SwitchToRight"];

Swift 5:斯威夫特 5:

//Animate Your Button From Left to Right
    let transition1: CATransition = CATransition()
    let timeFunc1 : CAMediaTimingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
    transition1.duration = 1.5
    transition1.timingFunction = timeFunc1
    transition1.type = CATransitionType.push
    transition1.subtype = CATransitionSubtype.fromLeft
    self.yourBtnRef.layer.add(transition1, forKey: kCATransition)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM