UIview animatewithDuration在swift中不起作用

时间:2015-07-24 07:08:51

标签: ios objective-c swift uiview jquery-animate

这似乎是一个非常奇怪的问题。我在storyBoard中有一个小图像视图,并添加了点击手势。在手势的动作我试图添加不同的图像视图。

目标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

                     }];

}

完全正常。

Swift Code -

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

    }
}

不起作用。我不知道我哪里错了。 :(

更新 -

首先我用“bigimageview.frame”更改框架,所以现在它在手势点击中显示图像视图。但没有动画。

所以我在主线程上移除了dipatch并且它是动画的。

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更改?有人请解释一下。

5 个答案:

答案 0 :(得分:0)

可能是你的延迟和动画时间相同所以试试这个

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: 1.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

    } }

答案 1 :(得分:0)

viewDidLoad()不是理想的观看地点。 viewDidAppear()方法可能有效。

答案 2 :(得分:0)

替换此代码

UIView.animateWithDuration(2.0, delay: 1.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

    } }

<强>与

UIView.animateWithDuration(2.0, delay: 1.0, options:UIViewAnimationOptions.CurveEaseInOut, animations: { () -> Void in
        dispatch_async(dispatch_get_main_queue()) {

            bigImageView.frame = CGRectMake(0, 0, 320, 500)
        }

    }) { (completed:Bool) -> Void in

    } }

答案 3 :(得分:0)

尝试使用dispatch_async()没有任何好处。 动画的工作方式只需在指定的动画块中设置属性即可;事实上,这些值是立即设置的,iOS会尽可能使用GPU来设置转换动画。 如果在不同的线程上运行代码块,它将超出动画的范围,因此只是立即生效。

答案 4 :(得分:0)

AnimationWithDuration块获取其所有数据(持续时间,选项等)。您的调度块只是单独迭代。这就是为什么它没有动画。原因很简单:由于堆栈系统,调度块内的所有内容都与其他代码分开迭代。帧停止,调度堆栈,执行,并且当它返回动画时:跳,它已经具有最终值并且不需要动画。

相关问题