你好,歡迎來到IOS教程網

 Ios教程網 >> IOS編程開發 >> IOS開發綜合 >> iOS中一個圖展的實現

iOS中一個圖展的實現

編輯:IOS開發綜合

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

\

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

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

那做這個東西的思路就很明晰了:首先這個類是個scrollview,然後在這個scrollview中添加imageview,然後給每個imageview添加相應的事件即可。<喎?/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





  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved