将视图移动到新的超级视图而不会闪烁 [英] Move a view to a new superview without flickering

查看:44
本文介绍了将视图移动到新的超级视图而不会闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为动画的一部分,我有一个视图,我想更深入地进入视图层次结构.(我以前将其移至视图层次结构的顶部,以执行出现在其他UI元素顶部的动画.)我正在使用此代码,该代码可以正确执行操作.

As part of an animation, I have a view that I want to move deeper into the view hierarchy. (I previously moved it to the top of the view hierarchy in order to perform an animation that appears on top of other UI elements.) I'm using this code, which does the correct thing.

CGRect rect = self.profileImage.frame;
UIView *sv = self.profileImage.superview;
[self.scrollview addSubview:self.profileImage];
rect = [self.scrollview convertRect:rect fromView:sv];
self.profileImage.frame = rect;

但是,当我这样做时,视图会闪烁.有什么主意如何避免闪烁?

However, the view flickers when I do this. Any ideas how to avoid the flicker?

推荐答案

在我的案例中,问题在于addSubview是在后台线程中执行的.您需要在主队列上执行更改,它应该可以正常工作.

In my case the problem was that addSubview was performed in a background thread. You need to perform the change on the main queue, and it should work just fine.

尝试

dispatch_async(dispatch_get_main_queue(), ^{
  CGRect rect = self.profileImage.frame;
  UIView *sv = self.profileImage.superview;
  [self.scrollview addSubview:self.profileImage];
  rect = [self.scrollview convertRect:rect fromView:sv];
  self.profileImage.frame = rect;
}

这篇关于将视图移动到新的超级视图而不会闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆