首页 > 代码库 > iOS中一个图展的实现
iOS中一个图展的实现
在app的首页一般都会有图展,用于显示广告,或者头条。典型的是网易的新闻客户端
如图,红框框的位置就是一个典型的图展,
熟悉iOS的人肯定知道,这个是个UIScrollview,里面加几张图片即可实现,当然下面的三个小点点也是必不可少的。
那做这个东西的思路就很明晰了:首先这个类是个scrollview,然后在这个scrollview中添加imageview,然后给每个imageview添加相应的事件即可。
源代码如下:
头文件:
// // GalleryView.h // Pitch // // Created by zhujinhui on 14-9-1. // Copyright (c) 2014年 zhujinhui. All rights reserved. // #import <UIKit/UIKit.h> /** * the protocol of the gallery */ @protocol GalleryDelegate <NSObject> -(void)galleryViewItemDidClicked:(int)index; @end /** gallery is used to show a lot of images */ @interface GalleryView : UIScrollView @property (assign ,nonatomic) id<GalleryDelegate> mDelegate; /** * set all the image to gallery */ -(void)setData:(NSArray *) data; @end
实现文件:
// // GalleryView.m // Pitch // // Created by zhujinhui on 14-9-1. // Copyright (c) 2014年 zhujinhui. All rights reserved. // #import "GalleryView.h" #define TAG_BTN_OFFSET 12345 @implementation GalleryView - (id)initWithFrame:(CGRect)frame { self = [super initWithFrame:frame]; if (self) { // Initialization code } return self; } /** * set all the image to gallery */ -(void)setData:(NSArray *) data{ //if data is not a array of string,it will throw exception @try { //remove all the subview from gallery view for (UIView *view in self.subviews) { [view removeFromSuperview]; } //add view to gallery for (int index = 0; index < [data count]; ++index) { NSString *imageName = data[index]; UIImage *img = [UIImage imageNamed:imageName]; UIImageView *imgv = [[UIImageView alloc]initWithImage:img]; CGRect frame = CGRectMake(index * 320, 0, 320, 150); [imgv setFrame:frame]; //add gesture to image imgv.userInteractionEnabled = YES; UITapGestureRecognizer *tapGestureRecognizer = [[UITapGestureRecognizer alloc]init]; [tapGestureRecognizer addTarget:self action:@selector(tapped:)]; [imgv addGestureRecognizer:tapGestureRecognizer]; //set tag imgv.tag = TAG_BTN_OFFSET + index; [self addSubview:imgv]; } } @catch (NSException *exception) { NSLog(@"%@",exception); } } -(BOOL)tapped:(UIGestureRecognizer *)gestureRecognizer{ //force convert index to integer int index = (int) (gestureRecognizer.view.tag - TAG_BTN_OFFSET); if (self.mDelegate) { if ([self.mDelegate respondsToSelector:@selector(galleryViewItemDidClicked:)]) { [self.mDelegate galleryViewItemDidClicked:index]; } }else{ NSLog(@"please set delegate"); } return TRUE; } -(void)awakeFromNib{ } /* // Only override drawRect: if you perform custom drawing. // An empty implementation adversely affects performance during animation. - (void)drawRect:(CGRect)rect { // Drawing code } */ @end
iOS中一个图展的实现
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。