开发者

Rearrange subviews in UIScrollView like App-Icons on Home-Screen

Hej folks,

I got a question to you: I got a UIScrollView in my application containing numerous subviews (all dire开发者_如何转开发ct subclasses of UIView). Now I want to give the user the possibility to rearrange these subviews inside the scrollview. Is there anyway to make it look like the way we all can change the icons on our homescreen (auto-rearrange)? I would really appreciate any help! Thanks in advance, Tim


Unless you use some custom tool kit (I'm not sure there exists a custom tool kit or API to achieve this), I think you should create them from scratch.

I'm also creating very similar thing you said. In an UIScrollView instance, I added small subviews and can order them only vertically by dragging. Also, I can take any subview to the outside of an UIScrollView instance. (a little bit tricky)

There are two ways to build them. (small subviews == icons)

1a) Implement touch events(gestureRecognizer or touchesBegan, Moved, Ended, Canceled) in a main subview of UIScrollView that contains many small subviews.

1b) Implement touch events in each small subview.

I chose 1b because I want to use small subviews not only in UIScrollView but also anywhere in my program. But with 1b, it was a little bit hard to prevent multitouch among these small views. Also I felt that with 1b, touch events in small subviews are more free from the complicated responder chains of an application.

2) Create a NSMutableArray instance that contains a list of small subviews.

3) Implement a function that displays a NSMutableArray instance onto a main view of an UIScrollView instance by enumerating it.

4) Implement a function that enumerates a NSMutableArray instance and checks whether a current touch position of small subview is in a frame of any small subview of a NSMutableArray. (or checks whether a current moving small subview's frame interesects a frame of any small subview) You can use CGRectContainsPoint or CGRectContainsRect functions from CGGeomtery.

5a) If yes, then you can just change frame of every small subview of a NSMutableArray.

5b) If the user releases a finger outside of frame of any small subview, then do nothing.

Simply using block-based animation methods(iOS4 and later) or begin/commit animation methods, you can create them like a home screen.


Very Good Example For Scrollview with Pagecontrol like iphone home Screen

https://github.com/jarada/myLauncher

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜