码迷,mamicode.com
首页 > 移动开发 > 详细

iOS的图表显示的实现

时间:2015-12-15 22:31:41      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:

在app通常有家居展览的照片,显示广告。或者头条新闻。通常网易新闻client

技术分享

如图,红框框的位置就是一个典型的图展,

熟悉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





iOS的图表显示的实现

标签:

原文地址:http://www.cnblogs.com/bhlsheji/p/5049652.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!