现在基本上每个应用的头部,都会是一个无限滚动显示图片的scrollview,然后点击图片可以跳转到不同的页面。今天我们来学习下如何封装一个这样的控件。
需求
- 三个imageview控件实现多张image的无限滚动
- 点击图片,可以拿到图片的信息给调用者使用
无限滚动效果图
点击图片事件
图片对应的信息一般由服务器返回,被封装到model,再传递给我们封装的无限滚动控件。当调用者通过代理方法实现回调,点击每张图片,我们会返回被点击图片对应的信息,这样调用者就可以拿到这些信息去做一些事情。
如下所示,返回了被点击图片的name和url
无限滚动scrollview封装
我们具体来看看如何封装一个无限滚动的uiscrollview,并实现点击事件。
下面给出了具体的实现代码,并且做了很详细的描述。
但是有两个方法比较难理解,我会单独用例子来讲解。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
InfiniteRollScrollView.h文件 ================================== #import <UIKit/UIKit.h> @class InfiniteRollScrollView; @protocol infiniteRollScrollViewDelegate <NSObject> @optional /** * 点击图片的回调事件 * * @param scrollView 一般传self * @param info 每张图片对应的model,由控制器使用imageModelInfoArray属性传递过来,再由该方法传递回调用者 */ -(void)infiniteRollScrollView:(InfiniteRollScrollView*)scrollView tapImageViewInfo:(id)info; @end @interface InfiniteRollScrollView : UIView /** * 图片的信息,每张图片对应一个model,需要控制器传递过来 */ @property (strong, nonatomic) NSMutableArray *imageModelInfoArray; /** * 需要显示的图片,需要控制器传递过来 */ @property (strong, nonatomic) NSArray *imageArray; /** * 是否竖屏显示scrollview,默认是no */ @property (assign, nonatomic, getter=isScrollDirectionPortrait) BOOL scrollDirectionPortrait; @property (weak, nonatomic, readonly) UIPageControl *pageControl; @property(assign,nonatomic)NSInteger ImageViewCount; @property(weak,nonatomic)id<infiniteRollScrollViewDelegate>delegate; @end |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 给调用者使用
无限滚动效果图点击图片事件图片对应的信息一般由服务器返回,被封装到model,再传递给我们封装的无限滚动控件。当调用者通过代理方法实现回调,点击每张图片,我们会返回被点击图片对应的信息,这样调用者就可以拿到这些信息去做一些事情。 无限滚动scrollview封装我们具体来看看如何封装一个无限滚动的uiscrollview,并实现点击事件。
|