Reputation: 23
According to Apple's Document, any touchable view should at least have a touchable area of 44 by 44 points.
I did some investigation, and it looks like a good solution is to over-write pointInside like following:
- (BOOL) pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
CGRect l_bounds = self.bounds;
float lengthOf44 = [self convertLengthFromScreen:44];
CGFloat l_widthDelta = lengthOf44 - l_bounds.size.width;
CGFloat l_heightDelta = lengthOf44 - l_bounds.size.height;
if (l_widthDelta < 0)
{
l_widthDelta = 0;
}
if (l_heightDelta < 0)
{
l_heightDelta = 0;
}
l_bounds = CGRectInset(l_bounds, -0.5 * l_widthDelta, -0.5 * l_heightDelta);
if (CGRectContainsPoint(l_bounds, point))
{
return YES;
}
return NO;
}
However, this solution has many draw backs. Given a small view having a 5 by 5 pixel size.
Suppose the small view is very close to the edge of super view, then this method will cause a half of the extend touchable area outside the superview, and The touchable area is actually 22 by 44. In that case I need to further overwrite superview's pointInside.
Suppose we have three small view like which are very close to each other, Then the upper view will always win the touch. Ideally, I want the view closest to the touch point win.
Is there a better or more elegant solution other than this please?
To better illustrate my concerns, let me give an example: Everyone should familiar with iOS's Copy Blue Hint Dot (not sure if I am using the correct word, but please see the image). https://i.sstatic.net/FtjYi.png
Suppose we are going to implement it (the copy blue dot). For that blue dot, I have no idea how Apple implemented. However, I assume that blue dot is a kind of UIImageView, and assume it is a subview of some UITextView (this may be not sure). Can that UIImageView response to touch? In my solution, it can response touch by overwriting pointInSide. My two question here in this context can be translated to be:
Upvotes: 1
Views: 4412
Reputation: 13459
You are taking the wrong aproach, first you should not mess with the touch detection from the ios, it is preconfigured and optimized to function correctly with finger touch, thats why if u touch you usually get the reading a bit higher than where you actually touched.
It seems you want to do some sort of "where did the user touched in a place", If i was you i would get a uiview for example which is my complete touchable area and just add the rest of the elements there, you can put them wherever you want, they can even overlap or anything, just make sure the superview is the one receiving the touches and do some math to see which one was the closest to the elements center. Even if they are buttons you can disable the interaction and programatically show their state changes. as if they were pressed or something.
To restate my point. Use just 1 big touch area and do the math for the rest, in this way you will have complete control over the touch areas and such.
(Then again i could be completely misunderstanding your purpose)
Upvotes: 0
Reputation: 3805
You missed the point of the docs.
Apple UI guidelines suggest that you do not make touchable areas (which are usually represented by a view of some kind, button, image whatever) less than 44X44 for usability reasons. (the size of a fingertip)
If you have a view 5X5, its not the right view to receive touch events and you should use a super view of that view to receive the touch events for the child.
(edited to remove my incorrect statement that this is not possible)
Upvotes: 1