UI第五天:設計模式、⼿勢識別器

⼀、target/action設計模式
耦合是衡量⼀個程序寫的好壞的標準之⼀,
耦合是衡量模塊與模塊之間關聯程度的指標 “⾼內聚,
低耦合”是⾯向對象編程的核⼼思想。





⼆、代理設計模式
當⼀個類的某些功能需要被別⼈來實現,但是既不明確是些什麼 功能,⼜不明確誰來實現這些功能的時候,委託模式就可以派上⽤ 場。
⺫的是爲了降低類之間的耦合性。
delegate也是⽤來解耦的,它不再簡簡單單讓⺫標去執⾏⼀個動 作了
⽽是delegate去處理⼀些列事件、就像UITextFieldDelegate⼀ 樣,能監測將要開始編輯,已經開始編輯、return按鈕點擊等等。
//告訴文件這是一個類
@class UIImageButton;
@protocol UIImageButtonDelegate <NSObject>
-(
void)buttonImageViewClick:(UIImageButton *)imagebutton;

@end

@interface UIImageButton : UIImageView
@property(nonatomic,retain)id target;
@property(nonatomic,assign)SEL action;
//添加代理屬性
//面試題 代理屬性 爲什麼要聲明成assign
//防止循環引用 從而造成的內存泄漏

/*
 
假如 AB的代理 B也是A的代理 如果是retain
 [[A alloc]init];
計數1
 [[B alloc]init];
計數1
 A.delegate = B;   B:2
 B.delegate = A;   A:2
 [A release];      1
 [B release];      1
 
這時 A B 都釋放不掉造成內存泄露
 */
@property(nonatomic,assign)id<UIImageButtonDelegate> delegate;

-(void)dealloc
{
    [
_target release];
    [
super dealloc];
}
-(
instancetype)initWithFrame:(CGRect)frame target:(id)target action:(SEL)action
{
   
self = [super initWithFrame:frame];
   
if (self) {
       
self.userInteractionEnabled = YES;
       
self.target = target;
       
self.action = action;
    }
   
return self;
}
-(
void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
   
}
-(
void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
   
}
-(
void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    [
self.target performSelector:self.action withObject:nil];
   
   
if ([_delegate respondsToSelector:@selector(buttonImageViewClick:)]) {
        [
_delegate buttonImageViewClick:self];
    }
   
//讓代理去調用代理方法
  
// [_delegate buttonImageViewClick];
   
}

  // 需求點擊imageview 實現換背景顏色 並且遵循MVC模式
//實現協議中的方法
//不管是target/Action設計模式還是代理模式還mvc模式中心只有一個解耦提高代碼的複用性
三、UIImageView
UIImageView是iOS中⽤於顯⽰圖⽚的類,iOS中⼏乎所有看到的 圖⽚,都是由這個類來顯⽰的。
使⽤initWithImage:⽅法,創建UIImageView對象 使⽤initWithContentOfFile:⽅法,創建⼀個UIImage對象




四、⼿勢識別器
UITapGestureRecognizer是輕拍⼿勢識別器,能識別輕拍操作
UILongPressGestureRecognizer是⻓按⼿勢識別器,能識別⻓按操作。
UIRotationGestureRecognizer是旋轉⼿勢識別器,能識別旋轉操作。
UIPinchGestureRecognizer是捏合⼿勢識別器,能識別捏合操作。
UIPanGestureRecognizer是平移⼿勢識別器,能識別拖拽操作。
UISwipeGestureRecognizer是輕掃⼿勢識別器,能識別拖拽操作。
UIScreenEdgePanGestureRecognizer是屏幕邊緣輕掃識別器,是iOS7中新增的⼿勢。

我們不會直接使⽤⼿勢識別器這個抽象⽗類,⽽是根據需要使⽤特定的⼿勢識別器創建對象。
1、創建UIxxxGestureRecognizer對象,使⽤initWithTarget:action:⽅法;
2、配置要識別的⼿勢的相關信息;
3、將⼿勢添加到某個視圖上;
4、實現⼿勢識別器⾥定義的⽅法
// 手勢類 UIGestureRecongnizer
   
// 這個類是個抽象類 其具體功能 交給子類去實習
   
//創建imageview 添加手勢
   
UIImageView *image = [[UIImageView alloc]initWithFrame:[UIScreen mainScreen].bounds];
    image.
image = [UIImage imageNamed:@"5.jpg"];
    [
self.view addSubview:image];
    image.
userInteractionEnabled = YES;
    [image
release];
   
   
   
//輕拍
   
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
   
//添加手勢到視圖上
    [image
addGestureRecognizer:tap];
    [tap
release];
   
//長按
   
//添加手勢的方法
   
//1.初始化手勢 添加手勢觸發調試的方法
   
//2.把手勢添加到視圖上
   
//3.釋放手勢
//    UILongPressGestureRecognizer *longpress = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longAction:)];
//    //設置長按時間
//    longpress.minimumPressDuration = 2.0;
//    //添加到視圖上
//    [image addGestureRecognizer:longpress];
//    //釋放
//    [longpress release];
    
//旋轉
//    UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(rotationAction:)];
//    [image addGestureRecognizer:rotation];
//    [rotation release];
   
//捏合
//    UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(pinchation:)];
//    [image addGestureRecognizer:pinch];
//    [pinch release];
//
//    //平移手勢
//    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
//    [image addGestureRecognizer:pan];
//    [pan release];
//   
   
//輕掃
   
UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];
   
//設置左右掃方向
    swipe.
direction = UISwipeGestureRecognizerDirectionLeft;
    [image
addGestureRecognizer:swipe];
    [swipe
release];
   
   
UIScreenEdgePanGestureRecognizer *edgepan = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(edgepan:)];
   
//設置從那個邊緣掃
    edgepan.
edges = UIRectEdgeRight;
    [image
addGestureRecognizer:edgepan];
    [edgepan
release];
}
// 實現輕拍方法
-(
void)tapAction:(UITapGestureRecognizer *)t
{
   
UIImageView *view = (UIImageView *)t.view;
    view.
image = [UIImage imageNamed:@"4.jpg"];
}
////實現長按方法
//-(void)longAction:(UILongPressGestureRecognizer *)l
//{
//    //判斷一下狀態 長按 只需觸發一次
//    if (l.state == UIGestureRecognizerStateBegan) {
//        UIImageView *view = (UIImageView *)l.view;
//        view.image = [UIImage imageNamed:@"3.jpg"];
//    }
//}
//-(void)rotationAction:(UIRotationGestureRecognizer *)r
//{
//    //形變屬性 transform
//    //參數一 要改變形變屬性的視圖
//    //參數二 根據弧度去創建
//    r.view.transform = CGAffineTransformRotate(r.view.transform,r.rotation);
//    //每次轉需要把旋轉的角度 重置爲0
//    //因爲要接替上一次的角度 開始旋轉
//    r.rotation = 0;
//}
//- (void)pinchation:(UIPinchGestureRecognizer *)p
//{
//    //根據縮放的刻度(比例改變形變屬性)
//   
//    p.view.transform = CGAffineTransformScale(p.view.transform, p.scale, p.scale);
//    p.scale = 1;
//    NSLog(@"123");
//}
// 實現平移方法的點(相對於要平移的視圖)
//- (void)pan:(UIPanGestureRecognizer *)pan
//{
//    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];
//}
-(
void)swipe:(UISwipeGestureRecognizer *)swipe
{
 
   
NSLog(@"123");
   
}
-(
void)edgepan:(UIScreenEdgePanGestureRecognizer *)edgepan
{
   
NSLog(@"321");
}
總結
target…action和delegate是很重要的設計模式,務必理解原理以 及熟練使⽤
⼿勢識別器是很常⽤的類,在⽇常開發中經常使⽤,需要牢記每 個⼿勢識別器的特點以及注意事項
transform是view的重要屬性,在屏幕旋轉⽅⾯⽤的⽐較多。
發佈了33 篇原創文章 · 獲贊 0 · 訪問量 7619
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章