An implementation of an image show in iOS

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.