On the homepage of the app, there will always be an exhibition showing ads, or headlines. Typical is NetEase's news client
, the position of the red box is a typical graphic exhibition,
Familiar with the iOS people must know, this is a uiscrollview, inside add a few pictures can be achieved, of course, the following three small dots is also essential.
The idea of doing this is very clear: first the class is a scrollview, then add ImageView to the ScrollView, and then add the corresponding event to each imageview.
The source code is as follows:
Header file:
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 <nsobj ect>-(void) galleryviewitemdidclicked: (int) index; @end/** Gallery is used-show a lot of images */@ Interface Galleryview:uiscrollview@property (assign, nonatomic) id<gallerydelegate> mdelegate;/** * Set all the IM Age to Gallery */-(void) SetData: (Nsarray *) data; @end
Implementation file:
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 are not a array of string,it would 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) (Gestur Erecognizer.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 Perform custom drawing.//an empty implementation adversely affects performance during animation.-(void) DrawRect: (CGRect ) rect{//Drawing code}*/@end
An implementation of an image show in iOS