我想知道UIImageViews
当彼此接触时如何在Xcode中交换两个的位置。
例子:
if (CGRectIntersectsRect(view1.frame, view2.frame)) {
[UIView animateWithDuration:0.2 animations:^{
CGRect view1Frame = view1.frame;
view1.frame = view2.frame;
view2.frame = view1Frame;
}];
}
不幸的是,这是行不通的,因为变量每次都会记住旧的位置。你能帮我吗?
请尝试以下方法:
CGRect view1Frame = view1.frame;
CGRect view2Frame = view2.frame;
if (CGRectIntersectsRect(view1.frame, view2.frame)) {
[UIView animateWithDuration:0.2 animations:^{
view1.frame = view2Frame;
view2.frame = view1Frame;
}];
}
我认为这应该可行,但我没有测试过,但通常情况下应该没问题...请让我知道...
另外:如果要直接更改块内的变量,以使它们在离开块后具有其他值,则需要使用__block声明它们(有关块的详细信息,我建议您使用apple docu http://developer.apple.com/library/ ios /#documentation / cocoa / conceptual / Blocks / Articles / bxVariables.html#// apple_ref / doc / uid / TP40007502-CH6-SW6)
问候
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句