[英]Working UIButton animations in Swift
I've been trying to get this snippit of code to work but I can't seem to get my UIButton to animate through images. 我一直在尝试使此代码段正常工作,但是我似乎无法让我的UIButton通过图像进行动画处理。
@IBOutlet weak var screenButton: UIButton!;
func animation(){
var animationButton : UIButton!;
var imageList = [UIImage]();
var image1:UIImage = UIImage(named:"3fingers-o-l-animation1.png")!;
var image2:UIImage = UIImage(named:"4fingers-o-l-animation1.png")!;
animationButtonscreenButton = screenButton;
imageList = [image1, image2];
animationButton!.imageView!.animationImages = imageList;
animationButton!.imageView!.animationDuration = 1.0;
animationButton!.imageView!.startAnimating();
}
After writing this code I've dragged a line from screenButton to the button I want to animate through the image in the Storyboard. 编写此代码后,我已从screenButton拖动一条线到要在情节提要中的图像上进行动画处理的按钮。 What am I doing wrong?
我究竟做错了什么?
What you need to do is to not re-initialize the UIButton
in the function animation
; 您需要做的是不要在函数
animation
重新初始化UIButton
; so, comment out this line: 因此,注释掉这一行:
//var animationButton : UIButton!
Besides just having an IBOutlet
, we need also an IBAction
to your function animation
. 除了仅拥有
IBOutlet
,我们还需要为您的函数animation
提供IBAction
。 Do the same thing as you do for your button by control dragging a "line" to the block of function (check IBAction
, not IBOutlet
). 通过控制将一条“线”拖动到功能块上,来完成与按钮相同的操作(检查
IBAction
,而不是IBOutlet
)。
A little walk-through while you are still in storyboard: 在您仍处于情节提要中的情况下,进行了一些逐步操作:
What you have is only one button named: animationButton
, which is an IBOutlet
from your storyboard to your code by control-dragging a "line". 您仅有的一个按钮名为:
animationButton
,这是通过拖曳“行”从情节提要到代码的IBOutlet
。
For your animation
function: Control-drag also from storyboard from the button a "line" to the block of the animation
function. 对于您的
animation
功能:按住Control键并将情节提要中的按钮从“线”拖动到animation
功能的块。
In addition, we need to set up an initial image in storyboard; 另外,我们需要在情节提要中设置初始图像; otherwise, the image property of the button is
nil
. 否则,按钮的图像属性为
nil
。
Please do so in storyboard by clicking on the right hand side "Attributes Inspector" and then set an image where it says "image". 请在情节提要中执行此操作,方法是单击右侧的“属性检查器”,然后在显示“图像”的位置设置图像。 As test, I set that up as "1.jpg".
作为测试,我将其设置为“ 1.jpg”。
Thus, you code should look like the following: 因此,您的代码应如下所示:
@IBOutlet weak var animationButton: UIButton!
@IBAction func animation()
{
print("button touched")
//var animationButton : UIButton!
var imageList = [UIImage]()
var image1:UIImage = UIImage(named:"1.jpg")!
var image2:UIImage = UIImage(named:"2.jpg")!
//animationButtonscreenButton = screenButton;
imageList = [image1, image2];
animationButton!.imageView!.animationImages = imageList
animationButton!.imageView!.animationDuration = 1.0
animationButton!.imageView!.startAnimating()
}
Just ran a quick test; 进行快速测试; it gets touch event and the images animate on the button.
它获得触摸事件,并且图像在按钮上进行动画处理。 Hope this helps.
希望这可以帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.