码迷,mamicode.com
首页 > 其他好文 > 详细

UI_05 设计模式、?势识别器

时间:2015-08-21 00:32:34      阅读:243      评论:0      收藏:0      [点我收藏+]

标签:

?、target/action设计模式

耦合

     耦合是衡量?个程序写的好坏的标准之?, 是衡量模块与模块之间关联程度的指标。 “?内聚,低耦合”是?向对象编程的核?思想。

使?target…action实现解耦

touchView.h:

@interface TouchView : UIView

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

- (
instancetype)initWithTarget:(id)target action:(SEL)action;

@end

touchView.m

- (instancetype)initWithTarget:(id)target action:(SEL)action
{
   
self = [super init];
   
if (self) {
       
self.target = target;
       
self.action = action;
    }
   
return self;
}

- (
void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
   
NSLog(@"clicked");
    [
self.target performSelector:self.action withObject:self];

}

RootViewController.h

#import "RootViewController.h"

#import "TouchView.h"
#import
"UIColor+RandomColor.h"

@interface RootViewController ()
@property (nonatomic, retain) TouchView *aTouchView;
@end

@implementation RootViewController

- (
void)dealloc
{
    [
_aTouchView release];
    [
super dealloc];
}

- (
void)viewDidLoad {
    [
super viewDidLoad];
   

    self.aTouchView = [[TouchView alloc] initWithTarget:self action:@selector(changeColor:)];

    //changeColor:方法的参数为TouchView.m中performSelector: withObject:方法的obj        

    //[_target performSelector:sel withObject:obj];

    //一个Target-Action在一个时间只能做一个事件

    self.aTouchView.frame = CGRectMake(100, 100, 200, 200);
   
self.aTouchView.backgroundColor = [UIColor redColor];

    [
self.view addSubview:_aTouchView];
    [
self.aTouchView release];

   
self.view.backgroundColor = [UIColor blueColor];
}

- (
void)changeColor:(TouchView *)view
{
    view.
backgroundColor = [UIColor randomColor];

}

@end



?、代理设计模式

delegate也是?来解耦的,它不再简简单单让?标去执??个动 作了,?是delegate去处理?些列事件、就像UITextFieldDelegate? 样,能监测将要开始编辑,已经开始编辑、return按钮点击等等。

通过使用代理设计模式,实现开始点击自定义视图touchView时变换颜色,结束点击时变换位置

TouchViewDelegate.h

#import <Foundation/Foundation.h>
@class TouchView;
//#import "TouchView.h"
@protocol TouchViewDelegate <NSObject>

@optional

- (void)touchViewTouchBegan:(TouchView *)touchView;     //做饭


- (
void)touchViewTouchMoved:(TouchView *)touchView;

- (
void)touchViewTouchEnd:(TouchView *)touchView;

- (void)touchViewTouchCancel:(TouchView *)touchView;

@end

touchView.h

@interface TouchView : UIView

@property (nonatomic, assign) id<TouchViewDelegate> delegate;  //保姆

@end

toucheView.m

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event  // 饿了

{

#pragma mark -- Delegate --

    if ([self.delegate respondsToSelector:@selector(touchViewTouchBegan:)]) {
        [
self.delegate touchViewTouchBegan:self];
    }
   
if ([self.delegate respondsToSelector:@selector(touchViewTouchEnd:)]) {
        [
self.delegate touchViewTouchEnd:self];

    }

}

RootViewController.m

#import "RootViewController.h"
#import
"TouchView.h"

@interface RootViewController ()
{
   
TouchView *_touchView1;
   
TouchView *_touchView2;
}
@end

@implementation RootViewController

- (
void)viewDidLoad {
    [
super viewDidLoad];
   
   
_touchView1 = [[TouchView alloc] initWithFrame:CGRectMake(100, 100, 100, 100)];
   
_touchView1.backgroundColor = [UIColor blueColor];

    

#pragma mark -- Delegate --
    //设置代理

    _touchView1.delegate = self;

    [self.view addSubview:_touchView1];
    [
_touchView1 release];

}


#pragma mark -- Delegate --

- (void)touchViewTouchBegan:(TouchView *)touchView     //做饭

{

    touchView.backgroundColor = [UIColor colorWithRed:arc4random() %256 / 255.0 green:arc4random() %256 / 255.0 blue:arc4random() %256 / 255.0 alpha:arc4random() %100 / 100.0];;

}

- (void)touchViewTouchEnd:(TouchView *)touchView
{
    touchView.
center = CGPointMake(50, 50);

}

@end


利用家庭-保姆来理解:

     TouchView:     家庭类

  ContainerViewController:保姆类

  TouchViewDelegate:      协议

  



三、UIImageView

UIImageView是iOS中?于显?图?的类,iOS中?乎所有看到的 图?,都是由这个类来显?的。

下面用UIImageView分别演示如何添加静态和动态图片

注:以下代码写在RootViewController.m

#import "RootViewController.h"
#import
"UIColor+RandomColor.h"

@interface RootViewController ()
@property (nonatomic,retain) UIImageView *imgView;

@end

@implementation RootViewController

- (
void)dealloc
{
    [
_imgView release];

    [super dealloc];

}

- (
void)viewDidLoad {
    [
super viewDidLoad];
   

#pragma mark -- UImageView --

    self.imgView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"1.png"]];   

    // 如果设置_imgView.frame,则图片大小为frame大小,发生形变。若不设置,则frame的大小为图片的大小
//    _imgView.frame = CGRectMake(0, 0, 371, 600);
   
NSLog(@"%.f %.f", _imgView.frame.size.width, _imgView.frame.size.height);
    [
self.view addSubview:_imgView];

    [_imgView release];

#pragma mark -- 使用UIImageView播放动画图片gif --
    UIImageView * animationImage = [[UIImageView alloc] init];
    animationImage.
frame = CGRectMake(100, 350, 150, 150);
   
   
NSMutableArray * imagesArr = [NSMutableArray array];
   
for (int i = 1; i<4; i++) {
       
UIImage *image = [UIImage imageNamed:[NSString stringWithFormat:@"tu%d.tiff", i]];
        [imagesArr
addObject:image];
    }
   
    animationImage.
animationImages = imagesArr;
    animationImage.
animationDuration = 0.1;
    [animationImage
startAnimating];
//    [animationImage stopAnimating];
   
    [
self.view addSubview:animationImage];

    [animationImage release];

}


...

@end



四、?势识别器

     ?势识别器是对触摸事件做了封装,我们?需??去判断某个?势 是否触发,?势识别器本?起到了识别作?,我们把重?放在识别之 后要做什么操作上?。

     ?势识别器有7个?类,?旦指定的?势被识别,我们可以执?我们??定义好的操作。

  • 轻拍?势:UITapGestureRecognizer

  • 平移?势:UIPanGestureRecognizer

  • 轻扫?势: UISwipeGestureRecognizer

  • 捏合?势:UIPinchGestureRecognizer

  • 旋转?势:UIRotationGestureRecognizer

  • ?按?势:UILongPressGestureRecognizer

  • 屏幕边界平移?势: UIScreenEdgePanGestureRecognizer(iOS7+)

RootViewController.m 

@interface RootViewController ()
@property (nonatomic,retain) UIImageView *imgView;
@property (nonatomic, retain) UIView *tapView;
@end

@implementation RootViewController

- (
void)dealloc
{
    [
_imgView release];
    [
_tapView release];
    [
super dealloc];

}


- (void)viewDidLoad {

    [super viewDidLoad];   

#pragma mark -- UIGestureRecognizer --
    UIView * tapView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
    tapView.
backgroundColor = [UIColor grayColor];
   
self.tapView = tapView;
    [
self.view addSubview:tapView];
    [tapView
release];
   
//轻拍手势
   
UITapGestureRecognizer * tapGR = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapOnce:)];
   
//添加手势
    [
self.tapView addGestureRecognizer:tapGR];
    [tapGR
release];
   
   
//长按手势
   
UILongPressGestureRecognizer * longGR = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
    [
self.tapView addGestureRecognizer:longGR];
    [longGR
release];
   
   
//平移手势
   
UIPanGestureRecognizer * panGR = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [
self.tapView addGestureRecognizer:panGR];
   
//    [animationImage addGestureRecognizer:panGR];
    [panGR
release];
   
   
//捏合手势
   
UIPinchGestureRecognizer * pinchGR = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinch:)];
    [
self.tapView addGestureRecognizer:pinchGR];
    [pinchGR
release];
   
   
//旋转
   
UIView * aView = [[UIView alloc] initWithFrame:CGRectMake(0, 100, 100, 100)];
    aView.
backgroundColor = [UIColor brownColor];
    [
self.view addSubview:aView];
    [aView
release];
   
UIRotationGestureRecognizer * rotationGR = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotation:)];
    [aView
addGestureRecognizer:rotationGR];
    [rotationGR
release];

   

    //轻扫 

//    UISwipeGestureRecognizer
   
   
//屏幕边界平移手势

//    UIScreenEdgePanGestureRecognizer

}

- (void)tapOnce:(UITapGestureRecognizer *)GR
{
//    self.tapView.hidden = YES;
//    self.tapView.backgroundColor = [UIColor randomColor];
   
NSLog(@"");
}

- (
void)longPress:(UILongPressGestureRecognizer *)GR
{
    GR.
view.backgroundColor = [UIColor randomColor];
}

- (
void)pan:(UIPanGestureRecognizer *)GR
{
   
   
CGPoint translation = [GR translationInView:GR.view];
   
NSLog(@"%@", NSStringFromCGPoint(translation));
   
CGPoint center = GR.view.center;
    center.
x += translation.x;
    center.
y += translation.y;
    GR.
view.center = center;
   
    [GR
setTranslation:CGPointMake(0, 0) inView:GR.view];

}


- (
void)pinch:(UIPinchGestureRecognizer *)GR
{
   
    GR.
view.transform = CGAffineTransformScale(GR.view.transform, GR.scale, GR.scale);
    GR.
scale = 1;
   
}

- (
void)rotation:(UIRotationGestureRecognizer *)GR
{
   
//第二个参数是弧度
   
//180 3.1415926 M_PI
    GR.
view.transform = CGAffineTransformRotate(GR.view.transform, GR.rotation);
    GR.
rotation = 0;

}


...

@end

     transform是view的?个重要属性,它在矩阵层?上改变view的显? 状态,能实现view的缩放、旋转、平移等等功能。 


UI_05 设计模式、?势识别器

标签:

原文地址:http://my.oschina.net/zooyf/blog/495291

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