iOS 開發中的手勢開發詳解

{ ui

    [super viewDidLoad]; spa

// Do any additional setup after loading the view, typically from a nib. orm

    

    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect]; ip

    [btn setFrame:CGRectMake(50, 50, 100, 100)]; get

    [btn setBackgroundImage:[UIImage imageNamed:@"1.png"] forState:UIControlStateNormal]; it

    // 拖移的 Recognizer io

    UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]initWithTarget:self                                               action:@selector(handlePan:)]; require

    [btn addGestureRecognizer:panGestureRecognizer]; select

    

    

    UIView *tapView = [[UIView alloc] initWithFrame:CGRectMake(10, 50, 300, 300)]; im

    [tapView setBackgroundColor:[UIColor redColor]];

    // 單擊的 Recognizer

    UITapGestureRecognizer* singleRecognizer;

    singleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(SingleTap:)];

    //點擊的次數

    singleRecognizer.numberOfTapsRequired = 1; // 單擊

    //點擊的手指數

    singleRecognizer.numberOfTouchesRequired = 2;

    

    //給view添加一個手勢監測;

    [tapView addGestureRecognizer:singleRecognizer];

    

    

    // 雙擊的 Recognizer

    UITapGestureRecognizer* doubleRecognizer;

    doubleRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(DoubleTap:)];

    doubleRecognizer.numberOfTapsRequired = 2; // 雙擊

    //關鍵語句,給self.view添加一個手勢監測;

    [tapView addGestureRecognizer:doubleRecognizer];

    

    // 關鍵在這一行,雙擊手勢肯定監測失敗纔會觸發單擊手勢的相應操做

    [singleRecognizer requireGestureRecognizerToFail:doubleRecognizer];

    

    // 捏合的 Recognizer

    UIPinchGestureRecognizer *pinchGestureRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handlePinch:)];

    [tapView addGestureRecognizer:pinchGestureRecognizer];

    

    // 旋轉的 Recognizer

    UIRotationGestureRecognizer *rotateRecognizer = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(handleRotate:)];

    [tapView addGestureRecognizer:rotateRecognizer];

    

    // 長按的 Recognizer

    UILongPressGestureRecognizer *longPressRecognizer = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleLongPress:)];

    //設置長按時間間隔

    [longPressRecognizer setMinimumPressDuration:1.0];

    [tapView addGestureRecognizer:longPressRecognizer];

    

    // 滑動的 Recognizer

    UISwipeGestureRecognizer *swipeRecognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipe:)];

    //設置滑動方向

    [swipeRecognizer setDirection:UISwipeGestureRecognizerDirectionDown];

    [tapView addGestureRecognizer:swipeRecognizer];

    

    

    [self.view addSubview:tapView];

    [self.view addSubview:btn];

}

相關文章
相關標籤/搜索