简体   繁体   English

自定义UIView不接收触摸事件

[英]Custom UIView not receiving touch events

I am building a custom slider to my application. 我正在为我的应用程序构建自定义滑块。

I found a library on Github as inspiration. 我在Github上找到了一个图书馆作为灵感。

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: . 我已经创建了文件: NKSlider作为UIView的子类,并像库那样在其上实现了drawRect并实现了方法: touchesMoved:withEvent: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 .h

#import <UIKit/UIKit.h>

@interface NKHandler : UIView

@end

and .m .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
nk.userInteractionEnabled = YES;

If userInteractionEnabled trick not works maybe you should check the view's parentview autosizing attributes . 如果userInteractionEnabled技巧不起作用,则可能应检查视图的parentview自动调整大小属性

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 . 我按如下所示进行更改,因为情节提要板最初是在纵向模式下加载的,但我将其以横向显示,因此父级的视图高度变为0 px And even dow the view itself kept its height the parents become 0 px so view did not get the event. 甚至dow视图本身也保持其高度,因此父级变为0 px,因此视图未获得该事件。

在此处输入图片说明在此处输入图片说明

这可能是一个UICollectionViewCell情况,但是当我的自定义视图类意外地从代码中的UICollectionViewCell时,却遇到了这个问题,但是在XIB中,它使用的元素是UIView

I've seen this line cause trouble with UIView events: 我已经看到此行导致UIView事件出现问题:

contentView.translatesAutoresizingMaskIntoConstraints = NO;

This line can cause headaches after you've pushed and popped a view controller, using the pushViewController and popViewControllerAnimated methods. 使用pushViewController和popViewControllerAnimated方法推入并弹出视图控制器后,此行可能会引起头痛。 Once your contentView is displayed again, you may find event handling doesn't work. 一旦您的contentView再次显示,您可能会发现事件处理不起作用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM