0

I have subclass of UIView

@interface TBL_CardView : UIView

It it internally have UIImageView for card image.

I need to handle touching of card in following way:

  1. When I touch (and still hold) TBL_CardView I need to set orange borderColor.
  2. If I am stil on top of TBL_CardView I need to set red borderColor.
  3. If I am not on top of TBL_CardView, I have moved my finger, because I want to cancel touch, then borderColor is removed.

I know how to handle setting of borderColor:

self.layer.borderColor = [UIColor redColor].CGColor;
self.layer.borderWidth = 3.0f;

But I do not know what is the easiest way to implement thin button like behavior ?
Should I make it just as button, or use UIResponder or something else ?
What are pros and cons for each case ?

WebOrCode
  • 6,852
  • 9
  • 43
  • 70

1 Answers1

2
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"%s", __PRETTY_FUNCTION__);

    UITouch *touch = [touches anyObject];
    CGPoint touchLocation = [touch locationInView: self];
    BOOL isInside = [self pointInside: touchLocation withEvent: event];
    if (isInside)
    {
        //if logic
        NSLog(@"INSIDE");
    }
    else
    {
        //else logic
        NSLog(@"OUTSIDE");
    }
}

You could check out this post for information on how to implement button-like behaviour and respond to touches on a UIView or a subclass.

Touch Event on UIView

What I usually do myself, however, is put a transparent (clear color and no title, not alpha = 0.0) UIButton on top of my UIView matching it s size and responding to the events I need instead. Check if it fits your needs.

Community
  • 1
  • 1
F1ank3r
  • 199
  • 1
  • 11