您的位置:首页 > 产品设计 > UI/UE

UI基础-设计模式、手势识别器

2015-11-16 22:33 507 查看

target/action设计模式

耦合

耦合是衡量一个程序写的好坏的标准之一,

耦合是衡量模块与模块之间关联程度的指标

“高内聚,低耦合”是面向对象编程的核心思想。

使用target…action实现解耦

例如:点击imageView实现换背景颜色 并且遵循MVC设计模式

1.准备工作:新建工程,新建一个视图控制器,将其设置为根视图控制器

2.新建一个ButtonView继承于UIView

3.在ButtonView.h中声明两个属性(记得dealloc)

@property (nonatomic, retain) id target;
@property (nonatomic, assign) SEL action;


4.重写初始化方法

- (instancetype)initWithFrame:(CGRect)frame target:(id)target action:(SEL)action
{
self = [super initWithFrame:frame];
if (self) {
// 初始化时 对属性 进行赋值
self.target = target;
self.action = action;
}
return self;
}


5.添加触摸方法

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"开始触摸");
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"取消触摸");
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
NSLog(@"点击");
// 使用self.target对象调用 action方法
// 让一个对象 去调用这个对象类里的方法
//  Object 可携带的参数
[self.target performSelector:self.action withObject:self];
}
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
}


6.在根视图控制器的viewDidLoad中,创建ButtonView的对象

ButtonView *buttonView = [[ButtonView alloc] initWithFrame:CGRectMake(100, 100, 200, 200) target:self action:@selector(buttonViewClick:)];
buttonView.backgroundColor = [UIColor redColor];
[self.view addSubview:buttonView];
[buttonView release];


7.实现方法

- (void)buttonViewClick:(ButtonView *)buttonView
{
// 改变背景颜色
buttonView.backgroundColor = [UIColor grayColor];
}


这就是一个简单的target/action设计模式的实现

代理设计模式

当一个类的某些功能需要被别人来实现,但是既不明确是些什么

功能,又不明确谁来实现这些功能的时候,委托模式就可以派上用

场。

目的是为了降低类之间的耦合性

使用delegate实现解耦

例如: 点击imageView 实现换背景颜色 并且遵循MVC设计模式

1.准备工作:新建一个工程,新建一个视图控制器,并将其设置为根视图控制器

2.新建一个ButtonImageView继承于UIimageView

3.在ButtonView.h中创建一个协议

// 告诉文件这是个类
@class ButtonImageView;

// 创建一个协议
@protocol ButtonImageViewDelegate <NSObject>

// 捕获点击事件
- (void)buttonImageViewClick:(ButtonImageView *)buttonView;

@end


因为在最上面文件未声明之前,所以要告诉文件该类的名字

4.添加代理属性

@property (nonatomic, assign) id<ButtonImageViewDelegate> delegate;


问:为什么代理属性要声明成assign?

结论:防止循环引用 从而在成的内存泄露

理由:

假如A是B的代理 B也是A的代理 如果是 retain

[[A alloc ] init]; 计数 1

[[B alloc ] init]; 计数 1

A.delegate = B; B:2

B.delegate = A; A:2

[A release]; A:1

[B release]; B:1

这时 A B 都会释放不掉 造成内存泄露

5.在ButtonImageView中实现触摸方法

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

}

- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
// 安全保护
if ([_delegate respondsToSelector:@selector(buttonImageViewClick:)]) {
// 让代理去干活
// 让代理 去调用代理方法
[_delegate buttonImageViewClick:self];
}
}


6.在根视图控制器的.h中将ButtonImageView声明成属性(记得deaclloc)

@property (nonatomic, retain) UIButtonView *buttonView;


7.在根视图控制器的viewDidLoad中设置

self.buttonView = [[UIButtonView alloc] initWithFrame:[UIScreen mainScreen].bounds];
self.buttonView.backgroundColor = [UIColor redColor];
self.buttonView.delegate = self;
self.buttonView.userInteractionEnabled = YES;
[self.view addSubview:self.buttonView];
[self.buttonView release];


8.在根视图控制器的viewDidLoad中实现方法

- (void)UIButtonViewClick:(UIButtonView *)UIButtonView
{
NSLog(@"dian");
self.buttonView.backgroundColor = [UIColor greenColor];
}


UIImageView

UIImageView是iOS中用于显示图⽚片的类,iOS中几乎所有看到的 图片,都是由这个类来显示的。

手势识别器

准备工作:新建一个工程,新建一个视图控制器,并将其设置为根视图控制器

在viewDidLoad中:

创建ImageView 添加手势用

UIImageView *imageView = [[UIImageView alloc] initWithFrame:[UIScreen mainScreen].bounds];
imageView.image = [UIImage imageNamed:@"p7.jpg"];
[self.view addSubview:imageView];
imageView.userInteractionEnabled = YES;
[imageView release];


手势类 UIGestureRecognizer

这个类是个抽象类 其具体功能 交给子类去实现

1、轻拍

在viewDidLoad中:

// 轻拍
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];
// 添加手势到视图上
[imageView addGestureRecognizer:tap];
[tap release];


实现方法:

// 轻拍手势实现
- (void)tapAction:(UITapGestureRecognizer *)tap
{
NSLog(@"你拍我了....好疼!");
UIImageView *imageView = (UIImageView *)tap.view;
imageView.image = [UIImage imageNamed:@"5.jpg"];
}


2.长按

在viewDidLoad中:

// 长按手势
// 添加手势步骤
// 1.初始化手势 添加手势触发调用的方法
// 2.把手势添加到视图上
// 3.释放手势
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
// 设置长按时间
longPress.minimumPressDuration = 2.0;
// 添加到视图上
[imageView addGestureRecognizer:longPress];
[longPress release];


实现方法:

// 长按手势实现
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress
{
// 判断一下手势状态 长按 只需要触发一次
if (longPress.state == UIGestureRecognizerStateBegan) {
NSLog(@"你按我了....");
}
}


3.旋转手势:

在viewDidLoad中:

// 旋转手势
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotationAction:)];
[imageView addGestureRecognizer:rotation];
[rotation release];


实现方法:

// 旋转手势实现
- (void)rotationAction:(UIRotationGestureRecognizer *)rotation
{
// 形变属性 transform
// 参数一 要改变形变属性的视图
// 参数二 根据弧度去创建
rotation.view.transform = CGAffineTransformRotate(rotation.view.transform, rotation.rotation);
// 每次转需要把旋转的角度 重置为零
// 因为要接替上一次的角度 开始旋转
rotation.rotation = 0;
NSLog(@"不要转了...我晕了");
}


4、捏合手势

在viewDidLoad中:

UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
[imageView addGestureRecognizer:pinch];
[pinch release];


实现方法:

// 捏合手势实现
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch
{
NSLog(@"你捏疼我了");
// 根据缩放的刻度(比例)改变形变属性
// 根据手势捏合的比较 去改变形变属性
pinch.view.transform = CGAffineTransformScale(pinch.view.transform, pinch.scale, pinch.scale);
// 重置捏合的比例
pinch.scale = 1;
}


5.平移手势

在viewDidLoad中:

UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
[imageView addGestureRecognizer:pan];
[pan release];


实现方法:

// 平移手势实现
- (void)panAction:(UIPanGestureRecognizer *)pan
{
NSLog(@"平移");
// 获取平移的点
CGPoint p = [pan translationInView:pan.view];
// 根据这个点 改变形变属性
pan.view.transform = CGAffineTransformTranslate(pan.view.transform, p.x, p.y);
// 重置这个点
[pan setTranslation:CGPointMake(0, 0) inView:pan.view];
}


6.轻扫

在viewDidLoad中:

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swipeAction:)];
// 设置左右扫
swipe.direction = UISwipeGestureRecognizerDirectionLeft;
[imageView addGestureRecognizer:swipe];
[swipe release];


实现方法:

// 轻扫
- (void)swipeAction:(UISwipeGestureRecognizer *)swipe
{
NSLog(@"你左右不分啊");
}


7. 边缘扫

在viewDidLoad中:

UIScreenEdgePanGestureRecognizer *screenEdgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(screenEdgePanAction:)];
// 设置一下 从屏幕哪个边缘扫
screenEdgePan.edges = UIRectEdgeRight;
[imageView addGestureRecognizer:screenEdgePan];
[screenEdgePan release];


实现方法:

// 边缘扫
- (void)screenEdgePanAction:(UIScreenEdgePanGestureRecognizer *)screenEdgePan
{
NSLog(@"靠近屏幕边缘开始扫 才能触发");
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: