how would one replicate the effect on the iPhone when you rearrange app icons? It would need to avoid collisions and rearrange the UIViews while snapped to a grid.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
I've written some sample code to do something like this.
See my blog entry: Tiles, and the associated GitHub repository.
回答2:
Check out the MoveMe sample project in the SDK to see a basic example of dragging and animation.
You'll have to implement the logic yourself to figure out where to move the icons based on the geometry of your grid and the progress of the drag operation. Once you know where the icons need to go, animating the views only requires a few lines of code. You'll want to do something like this:
CGRect newFrame = iconView.frame;
[UIView beginAnimations:@"SnapToGrid" context:nil];
newFrame.origin = newOriginSnappedToGrid;
iconView.frame = newFrame;
[UIView commitAnimations];
Read the sub-section titled, "Animating Views" under "Window and Views" in the iPhone Application Programming Guide. Then check out the methods in the UIView class reference under the heading, "Animating Views".