(ios) 屏幕触摸总结

本文介绍了iOS开发中实现屏幕触控(包括单击、双击)、手势识别(如UIlabel点击事件)、屏幕晃动效果及图片滑动换页的具体方法。通过代码示例展示了如何响应触摸事件、设置手势识别器并实现摇一摇功能。

1  屏幕触控实现(单击 双击)

  [self becomeFirstResponder];
     //允许多点互动
     self.view.multipleTouchEnabled=TRUE;

实现事件部分

#pragma mark-
#pragma mark touch 

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

     //触摸开始
   
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{

    //移动
    
}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
  //结束
 UITouch *atouch=[touches anyObject];
    if(atouch.tapCount>=2)
    {
        //双击
    }
    else if(atouch.tapCount==1)
    {
    
       //单击
    }
}
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{


}

2 手势识别(UIlabel 点击事件实现)

  //设置图片的点击事件
    UITapGestureRecognizer *recongnizer=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(handleTapFrom:)];
recongnizer.numberOfTapsRequired=1;
     self.img.userInteractionEnabled=YES;
    [self.img addGestureRecognizer:recongnizer];
}

-(void)handleTapFrom:(UITapGestureRecognizer *)recognizer{
      [self updateDisplayValuesWithTip:@"tap" tapCount:1 touchCount:1];
}

 

 

3 屏幕晃动实现

//AppDelegate 中实现
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    application.applicationSupportsShakeToEdit = YES;
}

//或者代码中实现
 [[UIApplication sharedApplication] setApplicationSupportsShakeToEdit:YES];

//ViewController 中实现下面方法

- (void)motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event NS_AVAILABLE_IOS(3_0);
{

}
- (void)motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event NS_AVAILABLE_IOS(3_0)
{
    if (event.subtype == UIEventSubtypeMotionShake) {
        
        //摇一摇 行为
         
    }
}
- (void)motionCancelled:(UIEventSubtype)motion withEvent:(UIEvent *)event NS_AVAILABLE_IOS(3_0)
{

} 

 4 图片滑动换页

 

UISwipeGestureRecognizer *recognizer;
       self.img.userInteractionEnabled=YES;
    recognizer = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleSwipeFrom:)];
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
    [[self img] addGestureRecognizer:recognizer];
    
    
    
}
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer {
    NSLog(@"Swipe received.");
    
    if (recognizer.direction==UISwipeGestureRecognizerDirectionRight) {
        NSLog(@"swipe down");
        [UIView beginAnimations:nil context:nil];
        [UIView setAnimationDuration:2.0f];
        [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
        [UIView setAnimationRepeatAutoreverses:NO];
        [UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:self.view cache:YES];
        //界面变化部分
       //........
        
        [UIView commitAnimations];
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值