您好,登錄后才能下訂單哦!
UITouch觸摸與手勢
1. //延遲調用單擊事件,以便雙擊時可取消單擊調用
[selfperformSelector:@selector(singleTap) withObject:nilafterDelay:0.2];
2. //類方法取消單擊調用方法,及時調用雙擊方法
[NSObjectcancelPreviousPerformRequestsWithTarget:selfselector:@selector(singleTap) object:nil];
3.self.clipsToBounds= YES//當子視圖越界時,剪切子視圖
幾種常用的手勢
//創建手勢視圖
UIView *gestureView = [[UIViewalloc]initWithFrame:CGRectMake(10, 20, 300, 300)];
gestureView.backgroundColor = [UIColororangeColor];
UILabel *label = [[UILabelalloc]initWithFrame:CGRectMake(100, 400, 100, 50)];
label.text = @"0";
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor = [UIColorblueColor];
label.tag = 100;
[self.viewaddSubview:label];
[self.viewaddSubview:gestureView];
/******************點擊手勢*******************/
UITapGestureRecognizer *tap1 = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tap1Action:)];
//點擊的數量---點擊的次數
tap1.numberOfTapsRequired = 1;
//點擊的個數-----手指的個數
tap1.numberOfTouchesRequired = 1;
[gestureView addGestureRecognizer:tap1];
//單手雙擊
UITapGestureRecognizer *tap2 = [[UITapGestureRecognizeralloc]initWithTarget:selfaction:@selector(tap2Action:)];
tap2.numberOfTouchesRequired = 2;//雙手雙擊
tap2.numberOfTapsRequired = 2;
[gestureView addGestureRecognizer:tap2];
//如果tap2觸發了.則tap1則失效-----雙擊時,單擊失效
[tap1 requireGestureRecognizerToFail:tap2];
/******************輕掃手勢***********************/
UISwipeGestureRecognizer *swip1 = [[UISwipeGestureRecognizeralloc]initWithTarget:selfaction:@selector(swip1Action:)];
[gestureView addGestureRecognizer:swip1];
/******************平移手勢***********************/
UIPanGestureRecognizer *pan1 = [[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(pan1Action:)];
[gestureView addGestureRecognizer:pan1];
/******************長按手勢***********************/
UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizeralloc]initWithTarget:selfaction:@selector(longPressAction:)];
[gestureView addGestureRecognizer:longPress];
/******************旋轉手勢***********************/
UIRotationGestureRecognizer *rotation = [[UIRotationGestureRecognizeralloc]initWithTarget:selfaction:@selector(rotationAction:)];
[gestureView addGestureRecognizer:rotation];
/******************捏合手勢***********************/
UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizeralloc]initWithTarget:selfaction:@selector(pinchAction:)];
[gestureView addGestureRecognizer:pinch];
}
/******************點擊手勢*******************/
//單擊
- (void)tap1Action:(UITapGestureRecognizer *)tap1
{
NSLog(@"單擊");
UILabel *label = (UILabel *)[self.viewviewWithTag:100];
label.text = @"單擊";
}
//雙擊
- (void)tap2Action:(UITapGestureRecognizer *)tap2
{
NSLog(@"雙擊");
UILabel *label = (UILabel *)[self.viewviewWithTag:100];
label.text = @"雙擊";
}
/******************輕掃手勢***********************/
- (void)swip1Action:(UISwipeGestureRecognizer *)swip1
{
// NSLog(@"輕掃");
UILabel *label = (UILabel *)[self.viewviewWithTag:100];
if (swip1.direction == UISwipeGestureRecognizerDirectionRight) {
label.text = @"向右輕掃";
}elseif (swip1.direction == UISwipeGestureRecognizerDirectionLeft){
label.text = @"向左輕掃";
}elseif (swip1.direction == UISwipeGestureRecognizerDirectionUp){
label.text = @"向上輕掃";
}elseif (swip1.direction == UISwipeGestureRecognizerDirectionDown){
label.text = @"向下輕掃";
}
}
/******************平移手勢***********************/
- (void)pan1Action:(UIPanGestureRecognizer *)pan1
{
// UILabel *label = (UILabel *)[self.viewviewWithTag:100];
CGPoint p1 = [pan1 locationInView:pan1.view];
NSLog(@"平移的坐標是:%@",NSStringFromCGPoint(p1));
}
/******************長按手勢***********************/
- (void)longPressAction:(UILongPressGestureRecognizer *)longPress
{
UILabel *label = (UILabel *)[self.viewviewWithTag:100];
if (longPress.state == UIGestureRecognizerStateBegan) {
label.text = @"開始長按";
}elseif (longPress.state == UIGestureRecognizerStateEnded){
label.text = @"長按結束";
}
}
/******************旋轉手勢***********************/
- (void)rotationAction:(UIRotationGestureRecognizer *)rota
{
// UILabel *label = (UILabel *)[self.viewviewWithTag:100];
//旋轉的弧度
CGFloat r = rota.rotation;
NSLog(@"旋轉的弧度是:%.2f",r);
NSLog(@"旋轉的角度是:%.2f",180 * r / M_PI);
}
/******************捏合手勢***********************/
- (void)pinchAction:(UIPinchGestureRecognizer *)pinch
{
//獲得捏合的比例
CGFloat scale = pinch.scale;
pinch.view.transform = CGAffineTransformMakeScale(scale, scale);
}
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。