學習iPhone UIKit 8

來源:互聯網
上載者:User

學習iPhone UIKit 8

1、UIResponder,畫面觸摸的檢測;

2、標籤觸摸的檢測;

#import <UIKit/UIKit.h>

@interface TouchableLabel : UILabel {
    
}

@end
@implementation TouchableLabel

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UIAlertView* alert = [ [ [ UIAlertView alloc ] initWithTitle: nil message: @"I'm a label!" delegate: nil cancelButtonTitle: nil otherButtonTitles: @"OK" , nil ] autorelease ];
    [ alert show ];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [ UIColor whiteColor ];
    //將新建立的標籤布置在畫面上
    TouchableLabel* label = [ [ [ TouchableLabel alloc ] init ] autorelease ];
    label.frame = CGRectMake( 60, 100, 200, 50 );
    label.text = @"Touch me!";
    label.textAlignment = UITextAlignmentCenter;
    label.backgroundColor = [ UIColor grayColor ];
    
    //必須將 userInteractionEnabled 屬性設定成YES , 預設為 NO
    label.userInteractionEnabled = YES;
    [ self.view addSubview: label ];
}

3、響應鏈;

#import <UIKit/UIKit.h>

@interface TouchableLabelForResponder : UILabel {
    
}

@end

#import "TouchableLabelForResponder.h"

@implementation TouchableLabelForResponder

- (id)init
{
    if ( (self = [ super init ] ) )
    {
        //將 userInteractionEnabled 設定成YES
        self.userInteractionEnabled = YES;
        //設定自動調整,以免充分設定
        self.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleTopMargin | UIViewAutoresizingFlexibleBottomMargin | UIViewAutoresizingFlexibleRightMargin | UIViewAutoresizingFlexibleLeftMargin;
    }
    return self;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //檢測到觸摸後,首先將標籤標題輸出到日誌中
    NSLog( @"%@",self.text );
    //但是,事件直接傳導到下一個響應控制項
    [ self.nextResponder touchesBegan: touches withEvent: event ];
}

4、二次觸碰;

#import <UIKit/UIKit.h>

@interface UIKitPrjDoubleTap : UIViewController {
    @private
    BOOL singleTapReady_;
}

@end

5、

6、手勢,拖動檢測;

#import <UIKit/UIKit.h>

@interface UIKitPrjDrag : UIViewController {
    @private
    //顯示圖片用的UIImageView
    UIImageView* character_;
    //移動標誌(控制移動)
    BOOL shouldWalk_;
    //當前手指位置
    CGPoint targetPoint_;
}

- (void)theCharacterWillWalk;

@end

7、滑動檢測;

#import <UIKit/UIKit.h>

//顯示滑動方向的枚舉類型定義
typedef enum
{
    kSlideNone,        //原狀態
    kSlideHorizontal,  //橫向滑動
    kSlideVertical,    //縱向滑動
} DirectionForSlide;

@interface UIKitPrJSlide : UIViewController {
        @private
    UILabel* label_;
    CGPoint touchBegan_;
    CGPoint labelOrigin_;
    DirectionForSlide direction_;
}

@end

8、快速滑動檢測;

#import <UIKit/UIKit.h>

@interface UIKitPrjFlick : UIViewController {
    @private
    NSTimeInterval timestampBegan_;
    CGPoint pointBegan_;
}

@end

9、多點觸摸,檢測多點觸摸;

10、檢測雙指滑動;
#import <UIKit/UIKit.h>

@interface UIKitPrjDoubleSlide : UIViewController {
    @private
    UILabel* label_;
}

@end

11、檢測擴大、縮小;

#import <UIKit/UIKit.h>

@interface UIKitPrjPinch : UIViewController {
    @private
    UIImageView* imageView_;
}

- (CGFloat)distanceWithPointA:(CGPoint)pointA pointB:(CGPoint)pointB;

@end

12、檢測震動,只有代碼;
//
//  LabelForMotion.h
//  likTwoHelloWorld
//
//  Created by apple on 13-1-30.
//  Copyright 2013年 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface LabelForMotion : UILabel {
    
}

@end
//
//  LabelForMotion.m
//  likTwoHelloWorld
//
//  Created by apple on 13-1-30.
//  Copyright 2013年 __MyCompanyName__. All rights reserved.
//

#import "LabelForMotion.h"

@implementation LabelForMotion

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

@end
//
//  UIKitPrjMotion.h
//  likTwoHelloWorld
//
//  Created by apple on 13-1-30.
//  Copyright 2013年 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>
#import "LabelForMotion.h"

@interface UIKitPrjMotion : UIViewController {
    
}

@end

13、使用加速度感應器實現滾球效果;
//  UIKitPrjAccelerometer.h
//  likTwoHelloWorld
//
//  Created by apple on 13-1-30.
//  Copyright 2013年 __MyCompanyName__. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface UIKitPrjAccelerometer : UIViewController <UIAccelerometerDelegate>{
    @private
    UIImageView* imageView_;              //圖片
    UIAccelerationValue speedX_;          //x軸方向的運動速度
    UIAccelerationValue speedY_;          //y軸方向的運動速度
}

@end

14、兩個按鈕的警告框;

15、關閉警告框,3秒後關閉警告框;
- (void)viewDidAppear:(BOOL)animated
{
    [ super viewDidAppear: animated ];
    UIAlertView* alert = [ [ [ UIAlertView alloc ] init ] autorelease ];
    alert.message = @"此資訊3秒後消失。";
    [ alert addButtonWithTitle: @"取消" ];
    alert.cancelButtonIndex = 0;
    [ alert show ];
    [ self performSelector:@selector(dismissAlert:) withObject: alert afterDelay: 3.0 ];
}

- (void)dismissAlert:(UIAlertView*)alert 
{
    if ( alert.visible )
    {
        [ alert dismissWithClickedButtonIndex: alert.cancelButtonIndex  animated: YES ];
    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.