要到另一个视图时UIimages移到中央 [英] UIimages move to the center when going to another view

查看:154
本文介绍了要到另一个视图时UIimages移到中央的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一拖n个墨滴样的应用程序。在这里你可以选择图像,随时随地拖动屏幕上!
我遇到的问题是,当你移动到另一个视图,所有图像重置为中心,当我回来了。
例如,如果我preSS一个按钮,带我到屏幕2,所有的拖图片我只是做了,就会搬回市中心。

当我已启用自动布局这只发生:(
我有我的所有图像中心开始了,所以我猜它与自动布局...

的东西

任何想法?!

下面是我拖动图像code的一个例子。

   - (IBAction为)handlePan:(UIPanGestureRecognizer *){识别CGPoint翻译= [识别translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
                                     recognizer.view.center.y + translation.y);
[识别setTranslation:CGPointMake(0,0)inView:self.view];
}


解决方案

您拖放code手动通过突变view.center财产配置的布局。

在自动布局启用自动布局需要设立布局的责任,所以需要设置view.center,view.bounds和view.frame(这实际上是刚刚从市中心和范围来计算的)全部责任。

因此​​,一旦自动布局被启用,但是你仍然可以view.center手动设定,自动布局将揍无论你做什么,它计算满足的地方,你有约束布局的下一次。

那么,你如何更新code,支持自动布局工作?如果你想使用自动布局,你需要做的是修改handlePan:方法,这样不是修改view.center它修改为准自动布局约束被用来计算view.center。这些的细节将取决于您的约束配置。但是,如果我们假定,例如,有一个NSLayoutContraint topSpaceConstraint,设置视图的顶部空间至上海华,和另一个NSLayoutConstraint leftSpaceConstraint,设置视图的左侧空间到上海华,则可以产生与

的效果相同

  view.center = CGPointMake(view.center.x + translation.x,
                          view.center.y + translation.y);

这是不是做类似

  topSpaceConstraint.constant = topSpaceConstraint.constant + translation.y;
leftSpaceConstraint.constant = leftSpaceConstraint.consant + translation.x;
[查看setNeedsLayout]
[查看layoutIfNeeded]

前两行更新的约束。最后两行可能会使自动布局系统重新计算所产生的框架和应用它向右走,而不是等到运行循环的下个回合。

另外,你也许可以用手动更新框,当你得到UIGestureRecognizerChanged事件,然后通过如上图所示,只有当你得到UIGestureRecognizerEnded事件升级的约束使结果永久脱身。这将是更好的性能,在此期间你会更新框架,而不是直接依靠自动排版系统上做了许多UIGestureRecognizerChanged事件至今。

I have a drag n drop kind of app. Where you can select images and drag them anywhere on the screen! The problem I'm running into is, when you move to another view, all the images reset to the center, when I return back. For example if I press a button to take me to screen 2, all the "dragged" images I just did, will move back to the center.

This only happens when I have AUTOLAYOUT enabled :( I have all my images start out in the center, so I'm guessing its something with autolayout...

Any ideas ?!

Here's an example of my drag image code.

 - (IBAction)handlePan:(UIPanGestureRecognizer *)recognizer {

CGPoint translation = [recognizer translationInView:self.view];
recognizer.view.center = CGPointMake(recognizer.view.center.x + translation.x,
                                     recognizer.view.center.y + translation.y);
[recognizer setTranslation:CGPointMake(0, 0) inView:self.view];
}

解决方案

Your drag and drop code is manually configuring the layout by mutating the view.center property.

Once auto layout is enabled, auto layout takes responsibility for setting up the layout, so it takes exclusive responsibility for setting view.center, view.bounds, and view.frame (which is actually just calculated from center and bounds).

So once auto layout is enabled, although you can still set view.center manually, auto layout will clobber whatever you do the next time it calculates the layout that satisfies the constraints you have in place.

So how do you update your code to work with auto layout? If you want to use auto layout, what you need to do is modify your handlePan: method so that instead of modifying view.center it modifies whichever auto layout constraint is being used to calculate view.center. The details of this will depend on your constraint configuration. But if we assume, for example, that there is an NSLayoutContraint topSpaceConstraint that sets the view's top space to the superview, and another NSLayoutConstraint leftSpaceConstraint that sets the view's left space to the superview, then you could produce the same effect as

view.center = CGPointMake(view.center.x + translation.x,
                          view.center.y+ translation.y);

by instead doing something like

topSpaceConstraint.constant = topSpaceConstraint.constant + translation.y;
leftSpaceConstraint.constant = leftSpaceConstraint.consant + translation.x;
[view setNeedsLayout];
[view layoutIfNeeded];

The first two lines update the constraints. The last two lines cause the auto layout system to recalculate the resulting frame and apply it right away, rather than waiting until the next turn of the run loop.

Alternatively, you might be able to get away with updating the frame manually when you get UIGestureRecognizerChanged events, and then making the result "permanent" by updating the constraints as shown above only once you get UIGestureRecognizerEnded event. That would be more performant, since during the many UIGestureRecognizerChanged events you'd be updating the frame directly rather than relying on the auto layout system to do it.

这篇关于要到另一个视图时UIimages移到中央的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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