iOS中一個圖展的實現

來源:互聯網
上載者:User

iOS中一個圖展的實現

在app的首頁一般都會有圖展,用於顯示廣告,或者頭條。典型的是網易的新聞用戶端


,紅框框的位置就是一個典型的圖展,

熟悉iOS的人肯定知道,這個是個UIScrollview,裡面加幾張圖片即可實現,當然下面的三個小點點也是必不可少的。

那做這個東西的思路就很明晰了:首先這個類是個scrollview,然後在這個scrollview中添加imageview,然後給每個imageview添加相應的事件即可。<喎?http://www.bkjia.com/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+1LS0+sLryOfPwqO6PC9wPgo8cD7Nt87EvP6jujwvcD4KPHA+PHByZSBjbGFzcz0="brush:java;">//// GalleryView.h// Pitch//// Created by zhujinhui on 14-9-1.// Copyright (c) 2014年 zhujinhui. All rights reserved.//#import /** * the protocol of the gallery */@protocol GalleryDelegate -(void)galleryViewItemDidClicked:(int)index;@end/** gallery is used to show a lot of images */@interface GalleryView : UIScrollView@property (assign ,nonatomic) id 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





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.