手势

来源:互联网 发布:公司网络监控 编辑:程序博客网 时间:2024/05/01 13:09

手势

userInteractionEnabled叫用户交互,如果没有打开的话没有办法通过手势的方式和它进行交互,如果没有确定是否打开,打印一下就可以,没打开的对象打印会有提醒(一般只有lable,imageView是关的)

  <span style="font-size:18px;"> self.image = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 300, 300)];    self.image.backgroundColor = [UIColor cyanColor];    [self.view addSubview:self.image];    [_image release];    self.image.image = [UIImage imageNamed:@"5.jpg"];    // userInteractionEnabled叫用户交互,如果没有打开的话没有办法通过手势的方式和它进行交互    // 如果不确定交互开没开.打印一下就可以,没打开的对象打印会有提示    self.image.userInteractionEnabled = YES; // 打印后的结果里userInteractionEnabled = NO消失//    NSLog(@"%@", self.imageView); // 一般中有imageView, lable是关的//    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];//    NSLog(@"%@", button);//    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(1, 1, 10, 10)];//    NSLog(@"%@", lable);</span>

手势1.点击

  <span style="font-size:18px;">UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapAction:)];//    // 把手势添加到视图上//    [self.imageView addGestureRecognizer:tap];//    // 内存管理//    [tap release];//    // 触发方法需要点击几次,默认是1//    tap.numberOfTapsRequired = 2;//    // 手指个数//    tap.numberOfTouchesRequired = 2;</span>

方法的实现

<span style="font-size:18px;">- (void)tapAction:(UITapGestureRecognizer *)tap{    NSLog(@"图片被点击");}</span>

2.长按

<span style="font-size:18px;">//    UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];//    [self.image addGestureRecognizer:longPress];//    //                   数组//    [longPress release];//    // 设置触发的最短时间//    longPress.minimumPressDuration = 2;//    // 设置可移动的范围//    longPress.allowableMovement = 300;//    // 可以按照对应的手势状态来完成判断//    if (longPress.state == UIGestureRecognizerStateBegan) {//        NSLog(@"开始了");//    }</span>

长按的方法

<span style="font-size:18px;">- (void)longPressAction:(UILongPressGestureRecognizer *)longPress{    NSLog(@"长按了");}</span>

旋转手势(Alt + 鼠标)

 <span style="font-size:18px;"> UIRotationGestureRecognizer *tapRotation = [[UIRotationGestureRecognizer alloc]initWithTarget:self action:@selector(tapRotation:)];    [self.image addGestureRecognizer:tapRotation];</span>

旋转的方法

<span style="font-size:18px;">- (void)tapRotation:(UIRotationGestureRecognizer *)tapRotation{    NSLog(@"旋转了");    //图片随手势一同旋转    self.image.transform = CGAffineTransformRotate(self.image.transform, tapRotation.rotation);//    self.image.transform = CGAffineTransformRotate(self.image.transform, tapRotation.rotation);    // 控制旋转的速度,速度是叠加的,越来越快,所以要赋初值为0    tapRotation.rotation = 0;}</span>

捏合手势

<span style="font-size:18px;">//    UIPinchGestureRecognizer *tapPinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(tapPinch:)];//    [self.image addGestureRecognizer:tapPinch];</span>

捏合的触发方法

<span style="font-size:18px;">- (void)tapPinch:(UIPinchGestureRecognizer *)tapPinch{    NSLog(@"捏合");    self.image.transform = CGAffineTransformScale(self.image.transform, tapPinch.scale, tapPinch.scale);    tapPinch.scale = 1;}</span>

拖拽手势

 <span style="font-size:18px;">//5.拖拽//    //    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];//    //    [self.image addGestureRecognizer:pan];</span>

拖拽的方法

<span style="font-size:18px;">// 拖拽- (void)pan:(UIPanGestureRecognizer *)pan{    NSLog(@"拖拽");    //先根据手势找到经过的点    CGPoint p = [pan translationInView:self.image];    //修改视图的transform    self.image.transform = CGAffineTransformTranslate(self.image.transform, p.x, p.y);    [pan setTranslation:CGPointZero inView:self.image];}</span>

轻扫手势

<span style="font-size:18px;">//6.轻扫手势//    UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(swipe:)];//    [self.image addGestureRecognizer:swipe];</span>

轻扫的方法

<span style="font-size:18px;">- (void)swipe:(UISwipeGestureRecognizer *)swipe{    NSLog(@"轻扫");}</span>

给视图添加一个毛玻璃效果

 <span style="font-size:18px;"> //给视图添加一个毛玻璃效果//    UIBlurEffect *effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleLight];//    UIVisualEffectView *view = [[UIVisualEffectView alloc]initWithEffect:effect];//    view.alpha = 0.5f;//    view.frame = CGRectMake(0, 0, 150, 150);//    //    [self.image addSubview:view];//    [view release];</span>

关于第一响应者的问题(点击下一项,输入框跳到下一个输入框)

称为第一响应者,textField是持续反应的,button是瞬间的事情

<span style="font-size:18px;">- (BOOL)textFieldShouldReturn:(UITextField *)textField{    if (textField == self.nameView.textField) {        [self.nameView.textField resignFirstResponder];        [self.codeView.textField becomeFirstResponder];    } else if (textField == self.codeView.textField){        [self.codeView.textField resignFirstResponder];        [self.checkView.textField becomeFirstResponder];    } else {        [self.checkView.textField resignFirstResponder];    }    return YES;}</span>

<span style="font-size:18px;">- (BOOL)textFieldShouldReturn:(UITextField *)textField{        [textField resignFirstResponder];    if(textField.tag == 10000){        [self.labelTextViewPW.textField becomeFirstResponder];    }    else if (textField.tag == 10001){        [self.labelTextViewPWAgain.textField becomeFirstResponder];    }        return YES;}</span>

屏幕边缘手势

 <span style="font-size:18px;">UIScreenEdgePanGestureRecognizer *screen = [[UIScreenEdgePanGestureRecognizer alloc]initWithTarget:self action:@selector(screen:)];    screen.edges = UIRectEdgeLeft;    [self.view addGestureRecognizer:screen];    [screen release];</span>

方法

<span style="font-size:18px;">- (void)screen:(UIScreenEdgePanGestureRecognizer *)sender{    NSLog(@"ok");}</span>


























































0 0
原创粉丝点击