UIview animatewithDuration不能快速运行 [英] UIview animatewithDuration does not works in swift
问题描述
这似乎是一个非常奇怪的问题.我在storyBoard中有一个小的图像视图,并向其中添加了轻按手势.关于手势的动作,我尝试添加其他图像视图.
This seems to be a very weird issue. I have a small image view in storyBoard and have added tap gesture to it. On the action of gesture I am trying to add a different image view.
目标C代码-
-(void)tapImage{
bigImageView = [[UIImageView alloc]init];
[bigImageView setImage:[UIImage imageNamed:@"main.png"]];
[bigImageView setFrame:CGRectMake(0, 0, 50, 50)];
[self.view addSubview:bigImageView];
[UIView animateWithDuration:2.0 delay:2.0 options:UIViewAnimationOptionCurveEaseInOut
animations:^(void) {
[bigImageView setFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)]; //Leave it empty
}
completion:^(BOOL finished){
// Your code goes here
}];
}
完全正常.
快捷代码-
func imageTapped()
{
println("Tapped on Image")
bigImageView = UIImageView(frame: CGRectMake(0, 0, 50, 50))
bigImageView.image = UIImage(named: "main")
self.view.addSubview(self.bigImageView)
UIView.animateWithDuration(2.0, delay: 2.0, options:UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
dispatch_async(dispatch_get_main_queue()) {
self.bigImageView = UIImageView(frame: CGRectMake(0, 0, 320, 500))
}
}) { (completed:Bool) -> Void in
}
}
不起作用.我不知道我在哪里错. :(
Does not work. I don't know where am I wrong. :(
更新-
首先,我使用"bigimageview.frame"更改框架,现在它正在手势轻击中显示图像视图.但是没有动画.
First I change the frame with "bigimageview.frame" so now it was displaying image view in gesture tap. But without animation.
因此,我在主线程上删除了Dipatch,并且正在制作动画.
So I removed dipatch on main thread and it was animating.
UIView.animateWithDuration(0.4, delay: 0.0, options:UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
// dispatch_async(dispatch_get_main_queue()) {
self.bigImageView.frame = CGRectMake(0, 0, 320, 500)
// }
}) { (completed:Bool) -> Void in
} }
但是仍然有疑问为什么会发生.我们是否不应该将UI更改放在闭包/块内的主线程上?请有人解释.
But Still question remains why it happened. Are we not suppose to put UI changes on main thread inside closure/block? Some one please explain.
推荐答案
可能是您的延迟时间和动画时间相同,因此请尝试
Might be your delay and animation time is same so try this
func imageTapped() {
print("Tapped on Image")
bigImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 50, height: 50))
bigImageView.image = UIImage(named: "main")
self.view.addSubview(self.bigImageView)
UIView.animate(withDuration: 2.0, delay: 1.0, options:UIView.AnimationOptions.curveEaseInOut, animations: { () -> Void in
DispatchQueue.main.async {
self.bigImageView = UIImageView(frame: CGRect(x: 0, y: 0, width: 320, height: 500))
}
}) { (completed:Bool) -> Void in
}
}
这篇关于UIview animatewithDuration不能快速运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!