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





郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。