UIButton TouchUpInside Touch Location

前端 未结 2 1627
滥情空心
滥情空心 2020-12-05 02:23

So I have a large UIButton, it is a UIButtonTypeCustom, and the button target is called for UIControlEventTouchUpInside. My question

相关标签:
2条回答
  • 2020-12-05 02:53
    UITouch *theTouch = [touches anyObject];
    CGPoint where = [theTouch locationInView:self];
    NSLog(@" touch at (%3.2f, %3.2f)", where.x, where.y);
    

    That's the right idea, except that this code is probably inside an action in your view controller, right? If so, then self refers to the view controller and not the button. You should be passing a pointer to the button into -locationInView:.

    Here's a tested action that you can try in your view controller:

    - (IBAction)buttonPressed:(id)sender forEvent:(UIEvent*)event
    {
        UIView *button = (UIView *)sender;
        UITouch *touch = [[event touchesForView:button] anyObject];
        CGPoint location = [touch locationInView:button];
        NSLog(@"Location in button: %f, %f", location.x, location.y);
    }
    
    0 讨论(0)
  • 2020-12-05 02:55

    For Swift 3.0:

    @IBAction func buyTap(_ sender: Any, forEvent event: UIEvent) 
    {
           let myButton:UIButton = sender as! UIButton
           let touches: Set<UITouch>? = event.touches(for: myButton)
           let touch: UITouch? = touches?.first
           let touchPoint: CGPoint? = touch?.location(in: myButton)
           print("touchPoint\(touchPoint)")  
    }
    
    0 讨论(0)
提交回复
热议问题