//
// guideView.h
// 07-功能引导-李洪强
//
// Created by vic fan on 16/1/4.
// Copyright © 2016年 李洪强. All rights reserved.
//
#import <UIKit/UIKit.h>
/**
* 定义代理协议
*/
@protocol guideViewDelegate <NSObject>
/**
* 定义可选实现的方法
*/
@optional
- (void)removeFromSuperView:(UIView *)view;
@end
@interface guideView : UIView
/**
* 定义代理对象
*/
@property(nonatomic,assign)id <guideViewDelegate> delegate;
@end
//
// guideView.m
// 07-功能引导-李洪强
//
// Created by vic fan on 16/1/4.
// Copyright © 2016年 李洪强. All rights reserved.
//
#import "guideView.h"
@interface guideView ()
@end;
@implementation guideView
//使用手码创建控件时,一般我们在该方法里动态添加组件&设置自身的一些属性。外界调用init方法,系统内部也会自动调用这个方法
- (instancetype)initWithFrame:(CGRect)frame{
self = [super initWithFrame:frame];
if (self) {
[self createView:frame];
}
return self;
}
-(void)createView:(CGRect )frame
{
self.backgroundColor = [UIColor blackColor];
self.alpha = 0.5;
/**
* 定义一个数组用来存放图片
*/
NSArray *picName = @[@"app_ass.png",@"dashang.png",@"jinzhang.png",@"zichan.png"];
/**
* for循环
*/
for(int i= 0; i< 4; i++){
/**
1 - 初始化imageView
*/
UIImageView *imageView = [[UIImageView alloc]init];
/**
* 2 - 把给每一个imageView设置图片
*/
imageView.image = [UIImage imageNamed:picName[i]];
/**
* 3 - 给每一个imageView设置tag值
*/
imageView.tag = i+100001;
/**
4 - 判断
* 如果是第一张图片的话
*/
if (i==0) {
imageView.hidden =NO;
imageView.frame = CGRectMake(50, 100, 150, 150);
}else if(i==1){
imageView.hidden =YES;
imageView.frame = CGRectMake(100, 200, 150, 150);
}else if(i==2){
imageView.hidden =YES;
imageView.frame = CGRectMake(200, 400, 150, 150);
}else if(i==3){
imageView.hidden =YES;
imageView.frame = CGRectMake(100, 500, 150, 150);
}
/**
* 5 - 可以与用户交互
*/
imageView.userInteractionEnabled = YES;
/**
6 - 创建一个手势
*/
UITapGestureRecognizer *tapGesture1=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageViewClicked:)];
// 7 -将手势添加到对应的imageView
[imageView addGestureRecognizer:tapGesture1];
// 8 - 把imageView1添加到view上
[self addSubview:imageView];
}
}
-(void)imageViewClicked:(UITapGestureRecognizer *)tapGR
{
UIImageView *imageView1 = (id)[self viewWithTag:100001];
UIImageView *imageView2 = (id)[self viewWithTag:100002];
UIImageView *imageView3 = (id)[self viewWithTag:100003];
UIImageView *imageView4 = (id)[self viewWithTag:100004];
if (tapGR.view.tag == 100001) {
imageView1.hidden = YES;
imageView2.hidden = NO;
imageView3.hidden = YES;
imageView4.hidden = YES;
}else if (tapGR.view.tag == 100002){
imageView1.hidden = YES;
imageView2.hidden = YES;
imageView3.hidden = NO;
imageView4.hidden = YES;
}else if (tapGR.view.tag == 100003){
imageView1.hidden = YES;
imageView2.hidden = YES;
imageView3.hidden = YES;
imageView4.hidden = NO;
}else if (tapGR.view.tag == 100004){
imageView1.hidden = YES;
imageView2.hidden = YES;
imageView3.hidden = YES;
imageView4.hidden = YES;
[tapGR.view removeFromSuperview];
// [self removeFromSuperview];
/**
* 判断代理
*/
if([self.delegate respondsToSelector:@selector(removeFromSuperView:)]){
[self.delegate removeFromSuperView:self];
}
}
}