• iOS的图表显示的实现


    在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
    





  • 相关阅读:
    快速实现一个带后端服务的 Vue 项目,用云开发Vue插件!
    做好内容安全检测,和风险说「再见」(下)!
    新能力|云调用支持微信支付啦!
    获奖结果公布|2020腾讯犀牛鸟云开发校园技术布道师养成计划
    SpringMVC原理及流程解析
    Mysql梳理-关于索引/引擎与锁
    写在庚子年之前
    Spring的BeanPostProcessor后置处理器与bean的生命周期
    AQS系列(六)- Semaphore的使用及原理
    AQS系列(五)- CountDownLatch的使用及原理
  • 原文地址:https://www.cnblogs.com/bhlsheji/p/5049652.html
Copyright © 2020-2023  润新知