Nicos Karalis
Nicos Karalis

Reputation: 3773

Custom UIView not receiving touch events

I am building a custom slider to my application.

I found a library on Github as inspiration.

I have created the file: NKSlider as a subclass of UIView and implemented on it the drawRect as the library did and implemented the methods: touchesMoved:withEvent: and touchesBegan:withEvent:. But none of the touches events are being called.

this is how i add this custom view to my view controller:

NKHandler *nk = [[NKHandler alloc] init];
nk.frame = CGRectMake(20, 20, 280, 36);
[self.view addSubview:nk];
[self.view addSubview:[[NKHandler alloc] initWithFrame:CGRectMake(20, 60, 280, 100)]];

and this are the classes. What am i doing wrong??

the .h

#import <UIKit/UIKit.h>

@interface NKHandler : UIView

@end

and .m

#import "NKHandler.h"

static inline CGPoint CGPointTopCenter(CGRect rect) {
  CGPoint p; p.x = rect.origin.x + (rect.size.width / 2); p.y = rect.origin.y; return p;
}
static inline CGPoint CGPointBottomCenter(CGRect rect) {
  CGPoint p; p.x = rect.origin.x + (rect.size.width / 2); p.y = rect.origin.y + rect.size.height; return p;
}
static inline CGPoint CGPointLeftCenter(CGRect rect) {
  CGPoint p; p.x = rect.origin.x; p.y = rect.origin.y + (rect.size.height / 2); return p;
}
static inline CGPoint CGPointRightCenter(CGRect rect) {
  CGPoint p; p.x = rect.origin.x + rect.size.width; p.y = rect.origin.y + (rect.size.height / 2); return p;
}

@implementation NKHandler {
  int _selectedStep;
}

-(id)init {
  self = [super init];
  if (self) {
    self.backgroundColor = [UIColor clearColor];

    self.clipsToBounds = NO;
    self.opaque = YES;

    _selectedStep = 5;   
  }
  return self;
}

-(void) setStep:(int) step {
  _selectedStep = step;
  [self setNeedsDisplay];
}

-(void)layoutSubviews {
  ... layout stuff ...
}
-(void)drawRect:(CGRect)rect {
  ... Boring drawing stuff ..
}

#pragma mark - Touch Handling

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
  NSLog(@"%s", __PRETTY_FUNCTION__);
  [self setStep:(_selectedStep+1)%11];
}

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

@end

Upvotes: 3

Views: 5813

Answers (4)

russes
russes

Reputation: 1120

I've seen this line cause trouble with UIView events:

contentView.translatesAutoresizingMaskIntoConstraints = NO;

This line can cause headaches after you've pushed and popped a view controller, using the pushViewController and popViewControllerAnimated methods. Once your contentView is displayed again, you may find event handling doesn't work.

Upvotes: 0

Patrick Lynch
Patrick Lynch

Reputation: 2782

This is probably a super edge case, but I had this problem when accidentally had my custom view class descend from UICollectionViewCell in code, but in the XIB it the element used was UIView.

Upvotes: 0

J&#225;nos
J&#225;nos

Reputation: 35050

If userInteractionEnabled trick not works maybe you should check the view's parentview autosizing attributes.

I changed it like this see below, because storyboard loaded it 'originally' in portrait mode, but I presented it in landscape, so the parent's view height become 0 px. And even dow the view itself kept its height the parents become 0 px so view did not get the event.

enter image description here enter image description here

Upvotes: 2

Bill Burgess
Bill Burgess

Reputation: 14154

nk.userInteractionEnabled = YES;

Upvotes: 4

Related Questions