UIView with a Png image - ignore touch in transparent areas

I have a circle PNG with a transparent background added as a spy. I use this type of method to rotate it:

CGPoint Location = [touch locationInView: self.view];

if(CGRectContainsPoint(wheelfrom.frame, location)) { } 

the problem is that the transparent region of the image is registered as part of the UIView. Is there a way to ignore this area when touched? Is there a better way to configure UIView to recognize transparency?

thanks!

+4
source share
2 answers

you can check the pixel color of the rbga image and see if there is a value (= alpha) == 0 (or <= aLowValue) ... as suggested by Igor Pchelko ...

but in your case it could be simpler ... you are using a two-dimensional circle, so just check how your finger click is far from the center of the circle and see if it has its radius ... just an application of the Pythagorean theorem ...

EDIT:

ok, so if you create a new class to subclass the UIButton button:

in YourButton.h:

 #import <UIKit/UIKit.h> #import <Foundation/Foundation.h> @interface YourButton : UIButton { } @end 

in YourButton.m just add this code:

  - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ UITouch *touch = [[event allTouches] anyObject]; CGPoint touchPoint = [touch locationInView:self]; NSLog(@"Touch x : %fy : %f", touchPoint.x, touchPoint.y); float circleRadius = self.frame.size.height / 2; // considering a circle inscricted in a quadRect (width = height) float deltaTouchOnCenterX = touchPoint.x - circleRadius; float deltaTouchOnCenterY = touchPoint.y - circleRadius; float touchDistanceFromCenter = sqrt((deltaTouchOnCenterX * deltaTouchOnCenterX) + (deltaTouchOnCenterY * deltaTouchOnCenterY) ); // or: float touchDistanceFromCenter = hypot(deltaTouchOnCenterX,deltaTouchOnCenterY); NSLog(@"sqrt_val: %f", touchDistanceFromCenter); NSLog(@"Touch on center x : %fy : %f", deltaTouchOnCenterX, deltaTouchOnCenterY); if (touchDistanceFromCenter <= circleRadius) { // your code here NSLog(@"___ OK: You are inside the circle!!"); } } 
+2
source

Try checking your UIImage (circle or something else) for opacity pixels.

To see the color of a pixel, see How to get pixel data from UIImage (Cocoa Touch) or CGImage (Core Graphics)?

0
source

Source: https://habr.com/ru/post/1343828/


All Articles