UI學習筆記---第w五天

來源:互聯網
上載者:User

標籤:style   blog   http   color   使用   strong   

target...action設計模式   代理設計模式   手勢辨識器

target...action設計模式 

耦合是衡量一個程式寫的好壞的標準之一,耦合是衡量模組與模組之間關聯程度的指標

"高內聚,低耦合"是物件導向編程的核心思想

方法參數一般都是對象本身

delegate實際模式

當?個類的某些功能需要被別?來實現,但是既不明確是些什麼功能,又不明確誰來實現這些功能的時候,委託模式就可以派上用場。

目的是為了降低類之間的耦合性

如何用delegate實現解耦

delegate 也是用來解耦的,它不再簡簡單單讓目標去執行一個動作了

而是delegate去處理一些列事件,就像UITextFieldDelegate一樣,能檢測將要開始編輯.return按鈕點擊等

使用情境:控制項有些列時間點,控制器可以實現這個代理方法,以便在適當的時機做適當的事

delegate是實現一些列事件

UIImageView

UIImageView是iOS中用於顯示圖片的類,iOS中幾乎所有看到的圖片都是這個類來顯示的

使用initWithImage:方法,建立UIImageView對象 

使用initWithContentOfFile:方法,建立一個UIImage對象

手勢辨識器

手勢辨識器是對觸摸事件做了封裝,我們?需?己去判斷某個手勢是否觸發,手勢辨識器本?身起到了識別作用,我們把重心放在識別之後要做什麼操作上面。

?勢辨識器是iOS中?比較抽象的?個類,?於識別?個手勢,所謂? 勢:有規律的觸摸。

手勢辨識器有七個類分別是輕拍手勢,平移手勢,撥動手勢,縮放手勢,旋轉手勢,長按手勢以及螢幕邊界平移手勢

一旦指定的手勢被識別,我們可以執行我們自己定義好的操作

如何使用辨識器

我們不會直接使??勢辨識器這個抽象父類,?是根據需要使用特定的手勢辨識器建立對象。

1、建立UIxxxGestureRecognizer對象,使用initWithTarget:action:?法; 

2、配置要識別的?勢的相關資訊;

3、將?勢添加到某個視圖上;

4、實現手勢辨識器?裡定義的方法

view的transform屬性

transform是view的一個重要屬性,它在矩陣層面上改變view的顯示狀態.能實現view的縮放.旋轉.平移等功能

控制器中一部分手勢代碼

 1 //單擊 2 //- (void)changeColor:(UITapGestureRecognizer *)tap 3 // 4 //{ 5 //    self.view.backgroundColor = [UIColor randomColor]; 6 //    NSLog(@"哈哈哈哈"); 7 //} 8 - (void)changeColor:(UISwipeGestureRecognizer *)swipe 9 10 {11     self.view.backgroundColor = [UIColor randomColor];12     NSLog(@"hello!!!!!");13 }14 //平移15 - (void)pan:(UIPanGestureRecognizer *)pan16 {17     CGPoint p = [pan translationInView:pan.view];18     NSLog(@"__________________%@",NSStringFromCGPoint(p));19     pan.view.transform = CGAffineTransformMakeTranslation(p.x, p.y);20 }21 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil22 {23     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];24     if (self) {25         // Custom initialization26     }27     return self;28 }29 30 - (void)viewDidLoad31 {32     [super viewDidLoad];33     self.view.backgroundColor = [UIColor redColor];34     //對於target..action這種設計模式,action中方法的參數始終是當前對象(tap)35     UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(changeColor:)];36     tap.numberOfTapsRequired = 2;37     //手勢通常是與視圖綁定的,用於識別這個視圖上是否出現過指定的手勢,如果出現過,會觸發target的action事件38     [self.view addGestureRecognizer:tap];39     [tap release];40     //左右輕掃41     UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(changeColor:)];42     swipe.direction = UISwipeGestureRecognizerDirectionRight | UISwipeGestureRecognizerDirectionLeft43    ;44     45     [self.view addGestureRecognizer:swipe];46     [swipe release];47     //上下輕掃48     UISwipeGestureRecognizer *swipe1 = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(changeColor:)];49     swipe1.direction = UISwipeGestureRecognizerDirectionUp | UISwipeGestureRecognizerDirectionDown50     ;51     52     [self.view addGestureRecognizer:swipe1];53     [swipe1 release];54     55     56     57     //平移58 //    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];59 //    [self.view addGestureRecognizer:pan];60 //    [pan release];

單擊改變顏色,雙擊移動位置

 1 //單擊改變顏色實現方法 2 - (void)single:(TouchView *)touchview 3 { 4     CGFloat red = arc4random()%256/255.0; 5     CGFloat green = arc4random()%256/255.0; 6     CGFloat blue = arc4random() %256/255.0; 7     self.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1]; 8 } 9 //雙擊移動位置實現方法10 - (void)doubletap:(TouchView *)touchview11 {12     //視圖寬度13     NSInteger width = self.frame.size.width;14     //視圖高度15     NSInteger height = self.frame.size.height;16     //移動後的x座標17     CGFloat x = arc4random() % (321-width)+width/2;18     //移動後的y座標19     CGFloat y = arc4random() % (481 -height)+height/2;20     self.center = CGPointMake(x, y);21 }22 -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event23 {24     25     26     NSInteger tapcount = [[touches anyObject] tapCount];27     if (tapcount ==1) {28         [self performSelector:@selector(single:) withObject:nil afterDelay:0.3];//順延強制29     }30     if (tapcount ==2 ) {31         //點擊為2時取消單擊觸發的事件32         [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector(single:) object:nil];33         [self doubletap:nil];34 35     }36     37     38 }

 





聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.