78

How to increase the touch area of ​​UIView in iOS

 4 years ago
source link: https://www.codesd.com/item/how-to-increase-the-touch-area-of-uiview-in-ios.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to increase the touch area of ​​UIView in iOS

advertisements

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.

  1. 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.

  2. 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). http://i.stack.imgur.com/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:

  1. what if the word we are selected is too close to the UITextView edge? So the ideal touch area of that blue dot might be even outside the superview (UITextView).
  2. what if the upper blue dot is too close to lower blue dot and we drag from middle point of two dots (it looks like lower blue dot have a higher chance to win.)

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)


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK