이벤트 및 제스처

12611 단어 IOS 기본 컨트롤
code
보기를 손가락에 따라 움직이게 하다
이전에 CGPoint 속성 설정
@property (nonatomic, assign) CGPoint myPoint;

- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    //         
    // 1.   touch  
    UITouch *touch = [touches anyObject];
    self.myPoint = [touch locationInView: self];

}

- (void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    [super touchesMoved:touches withEvent:event];
    UITouch *touch = [touches anyObject];
    CGPoint newPoint = [touch locationInView: self];
    //           
    CGFloat x = newPoint.x - self.myPoint.x;
    CGFloat y = newPoint.y - self.myPoint.y;
    self.center = CGPointMake(self.center.x + x , self.center.y + y);
}

이벤트
터치
- (void) touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    NSLog(@"    ");
    //     textField       ==》     ,    

}
- (void) touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesEnded:touches withEvent:event];
    NSLog(@"    ");
}
- (void) touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesCancelled:touches withEvent:event];
    NSLog(@"     ");
}
- (void) touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesMoved:touches withEvent:event];
    NSLog(@"    ");
}

흔들다
흔들어 봐. 시작.
- (void) motionBegan:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    [super motionBegan:motion withEvent:event];

}

흔들어서 끝.
- (void) motionEnded:(UIEventSubtype)motion withEvent:(UIEvent *)event {
    [super motionEnded:motion withEvent:event];

}

손짓
그림에 제스처를 추가하는 등 동작을 하려면 먼저 대상의 사용자와 상호작용을 켜야 한다
self.viewNeedToAddGesture.userInteractionEnabled = YES;

살살
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector(tapAction:)];
    [self.viewNeedToTap addGestureRecognizer: tap];
    [tap release];

- (void) tapAction: (UITapGestureRecognizer *) tap {
    NSLog(@"      ");
} 

property
  • numberOfTapsRequired tap
  • numberOfTouchesRequired

  • 길게 누르다
        UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPressAction:)];
        [self.viewNeedToLongPress addGestureRecognizer: longPress];
        [longPress release];
    
    - (void) longPressAction: (UITapGestureRecognizer *)longPress {
        //                   
        if (longPress.state == UIGestureRecognizerStateBegan) {
            NSLog(@"      ");
        }
    }

    property
  • minimumPressDuration
  • allowableMovement

  • 빙글빙글 돌다
        UIRotationGestureRecognizer *rotate = [[UIRotationGestureRecognizer alloc] initWithTarget:self action:@selector(rotateAction:)];
        [self.viewNeedToRotate addGestureRecognizer: rotate];
        [rotate release];
    
    - (void) rotateAction: (UIRotationGestureRecognizer *)rotate {
        //     ,       
        UIImageView *viewNeedToRotate = (UIImageView *)rotate.view;
        //        
        self.viewNeedToRotate.transform = CGAffineTransformMakeRotation(rotate.rotation);
    }

    반죽하다
        UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchAction:)];
        [self.viewNeedToPinch addGestureRecognizer: pinch];
        [pinch release];
    
    - (void) pinchAction: (UIPinchGestureRecognizer *)pinch {
        self.viewNeedToPinch.transform = CGAffineTransformMakeScale(pinch.scale, pinch.scale);
    }

    잡아당기다
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
        [self.viewNeedToPan addGestureRecognizer: pan];
        [pan release];

    첫번째 방법
    
    - (void) pan: (UIPanGestureRecognizer *) pan {
        //        
        CGPoint point = [pan translationInView:pan.view];
        pan.view.transform = CGAffineTransformTranslate(pan.view.transform, point.x, point.y);
        [pan setTranslation:CGPointZero inView:pan.view];
    }

    두 번째 방법
    - (void) pan: (UIPanGestureRecognizer *) pan {
        //        
        CGPoint point = [pan translationInView:pan.view];
        self.viewNeedToPan.center = CGPointMake(self.viewNeedToPan.center.x + point.x, self.viewNeedToPan.center.y +point.y);
        [pan setTranslation:CGPointZero inView:self.viewNeedToPan];
    }

    보통 이렇게 써요.
    - (void) panAction: (UIPanGestureRecognizer *) pan {
        if (pan.state == UIGestureRecognizerStateBegan) {
    
        } else if (pan.state == UIGestureRecognizerStateChanged) {
            CGPoint point = [pan translationInView:pan.view];
            pan.view.transform = CGAffineTransformTranslate(pan.view.transform, point.x, point.y);
            [pan setTranslation:CGPointZero inView:pan.view];
        } else {
            //     
            pan.view.transform = CGAffineTransformIdentity;
        }
    }

    가볍게 쓸다
        UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget: self action:@selector(swipeAction:)];
        [self.viewNeedToSwipe addGestureRecognizer: swipe];
        [swipe release];
    
    - (void) swipe: (UISwipeGestureRecognizer *) swipe {
        if (swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
            NSLog(@"left");
        } else {
            NSLog(@"other direction");
        }
    }

    property
  • direction , . ,

  • 화면 경계 제스처
    UIScreenEdgePanGestureRecognizer

    좋은 웹페이지 즐겨찾기