UIGestureRecognizer

有兩種方式 (TBD)
  • UIResponder的四種方法,可以讓使用者自行設計手勢
    - (void)touch esBegan:(NSSet *)touches withEvent:(UIEvent *)event
    - (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
     
  • UIGestureRecognizer的方式,不用再自己一個一個去設計/解析,目前有6種
    - UILongPressGestureRecognizer
    - UIPanGestureRecognizer
    - UIPinchGestureRecognizer
    - UIRotationGestureRecognizer
    - UISwipeGestureRecognizer
    - UITapGestureRecognizer
  • 第一種方式的用法
    參考 iOS: how do you get the touchesBegan coordinates when a UIButton is triggered? 
    UIButton *mouse = [UIButton buttonWithType:UIButtonTypeCustom];
    [mouse setFrame:CGRectMake(90.0f, 55.0f, 118.0f, 118.0f)];
    [mouse setBackgroundImage:[UIImage imageNamed:@"mouse.png"] forState:UIControlStateNormal];
    [mouse setTitle:@"Mouse" forState:UIControlStateNormal];
    [mouse.titleLabel setFont:[UIFont fontWithName:@"Arial-BoldMT" size:20]];
    [mouse setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [mouse addTarget: self action: @selector(mouseClick:) forControlEvents: UIControlEventTouchDown];
    [parent addSubview:mouse];
    [mouse addTarget:self action:@selector(touchesBegan:withEvent:)  forControlEvents: UIControlEventTouchDown];
    [mouse addTarget:self action:@selector(touchesMoved:withEvent:) forControlEvents: UIControlEventTouchDragInside];
    [mouse addTarget:self action:@selector(touchesEnded:withEvent:)  forControlEvents: UIControlEventTouchUpInside | UIControlEventTouchUpOutside];
    -(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
    {
        NSLog(@"touchesBegan");
        UITouch *touch = [[event allTouches] anyObject];
        CGPoint touchPoint = [touch locationInView:parent];
        NSLog(@"Touch x : %f y : %f", touchPoint.x, touchPoint.y);
    }
    -(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
    {
        NSLog(@"touchesMoved");
        UITouch *touch = [[event allTouches] anyObject];
        CGPoint touchPoint = [touch locationInView:parent];
        NSLog(@"Touch x : %f y : %f", touchPoint.x, touchPoint.y);
        mouse.center = CGPointMake(touchPoint.x,touchPoint.y);
    }
    -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
    {
        NSLog(@"touchesEnded");
        UITouch *touch = [[event allTouches] anyObject];
        CGPoint touchPoint = [touch locationInView:parent];
        NSLog(@"Touch x : %f y : %f", touchPoint.x, touchPoint.y);
    }

轉至:http://ed32.blogspot.com/2011_07_31_archive.html

留言

熱門文章