经典指数          
原因
3210
浏览数
0
收藏数
 

IOS系统提供了那些手势?请选择一个自己写代码实现 UITapGestureRecognizer敲击手势(单击和双击) UIPanGestureRecognizer(拖动手势) UIPinchGestureRecognizer(缩放手势) UISwipeGestureRecognizer(擦碰手势) UIRotationGestureRecognizer(旋转手势) UILongPressGestureRecognizer(长按手势)

     举报   纠错  
 
切换
1 个答案

   

    UIView

*gestureView = [[UIView

alloc] initWithFrame:CGRectMake(50, 50, 300, 300)];

    gestureView.backgroundColor = [UIColor

blackColor];

    [self.view

addSubview:gestureView];

    

   

/*---------------点击手势-------------*/

    

   

UITapGestureRecognizer *tap1

= [[UITapGestureRecognizer

alloc

]

initWithTarget

:

self

action

:

@selector

(tap1Action:)];

   

//注意:一个点击手势,只能识别一种手势,单击和双击是不同的两个手势

   

//设置点击的数量

   

tap1.numberOfTapsRequired =

1

;

   

//设置点击的个数

   

tap1.numberOfTouchesRequired

=

1

;

    //往gestureView上添加一个手势

    [gestureView addGestureRecognizer:tap1];

    

   

UITapGestureRecognizer *tap2

= [[UITapGestureRecognizer

alloc

]

initWithTarget

:

self

action

:

@selector

(tap2Action:)];

   

tap2.numberOfTapsRequired =

2

;

    [gestureView addGestureRecognizer:tap2];

    

   

//如果参数中的手势出发了,则自身失效

    [tap1

requireGestureRecognizerToFail:tap2];

    

    

   

/*---------------轻扫手势-------------*/

   

UISwipeGestureRecognizer

*swipeGesture = [[UISwipeGestureRecognizer

alloc

]

initWithTarget

:

self

action

:

@selector

(swipe:)];

   

// 设置方向---向上轻扫

    swipeGesture.

direction

=

UISwipeGestureRecognizerDirectionUp;

    [gestureView addGestureRecognizer:swipeGesture];

    

    

   

/*---------------平移手势-------------*/

   

// 平移手势(滑动)

   

UIPanGestureRecognizer

*panGesture = [[UIPanGestureRecognizer

alloc

]

initWithTarget

:

self

action

:

@selector

(panAction:)];

    [gestureView addGestureRecognizer:panGesture];

    

    

   

/*---------------长按手势-------------*/

    // 长按手势

   

UILongPressGestureRecognizer

*longPressGesture = [[UILongPressGestureRecognizer

alloc

]

initWithTarget

:

self

action

:

@selector

(longPress:)];

   

//设置长安的最短时间

    longPressGesture.minimumPressDuration = 3;

    [gestureView addGestureRecognizer:longPressGesture];

    

   

/*---------------旋转手势-------------*/

    // 旋转手势

   

UIRotationGestureRecognizer

*rotationGesture = [[UIRotationGestureRecognizer

alloc

]

initWithTarget

:

self

action

:

@selector

(rotation:)];

    [gestureView addGestureRecognizer:rotationGesture];

    

   

/*----------捏合手势--------------*/

   

UIPinchGestureRecognizer

*pinch = [[UIPinchGestureRecognizer

alloc

]

initWithTarget

:

self

action

:

@selector

(pinchAction:)];

    [gestureView addGestureRecognizer:pinch];

}

//单击

- (void)tap1Action:(UITapGestureRecognizer *)tap1

{

    NSLog(@"

单击

");

}

//双击

- (void)tap2Action:(UITapGestureRecognizer *)tap2

{

    NSLog(@"

双击

");

}

//轻扫

- (

void

)swipe:(UISwipeGestureRecognizer *)swipe

{

   

if

(swipe.

direction

==

UISwipeGestureRecognizerDirectionUp) {

        NSLog(@"

向上清扫

");

    }

}

//平移

- (

void

)panAction:(UIPanGestureRecognizer *)pan

{

    CGPoint p = [pan

locationInView:pan.view];

    NSLog(

@"%@"

, NSStringFromCGPoint(p));

}

//长按

- (

void

)longPress:(UILongPressGestureRecognizer *)longPress

{

   

if

(longPress.

state

==

UIGestureRecognizerStateBegan) {

        NSLog(@"

开始长按

");

    }

else

if

(longPress.

state

==

UIGestureRecognizerStateEnded) {

        NSLog(@"

结束长按

");

    }

}

//旋转

- (

void

)rotation:(UIRotationGestureRecognizer *)rotation

{

    //获取旋转角度---弧度

    CGFloat r =

rotation.rotation;

    

    //弧度转角度

    //180/Pi = x/r

    NSLog(@"r is %.2f", 180 / M_PI * r);

}

//捏合

- (void)pinchAction:(UIPinchGestureRecognizer

*)pinch

{

   

//获得捏合的比例

    CGFloat scale =

pinch.scale;

    

//    pinch.view.transform =

CGAffineTransformScale(pinch.view.transform, scale, scale);

    pinch.view.transform = CGAffineTransformMakeScale(scale,

scale);

    

}

 
切换
撰写答案