简体   繁体   English

iOS键盘扩展检测点击

[英]iOS Keyboard Extension detect tap

I am building a custom keyboard and adding a scroll view of images to it. 我正在构建自定义键盘,并向其中添加图像的滚动视图。 I am trying to detect taps but it is not working. 我正在尝试检测水龙头,但是它不起作用。 Any suggestions? 有什么建议么? I have tried adding a action to a UIButton within the scroll view as well with no luck. 我试过在滚动视图中向UIButton添加动作,也没有运气。 This code displays the images in a scrollview fine however it won't detect taps. 这段代码可以在滚动视图中很好地显示图像,但是不会检测到点击。 CODE BELOW 下面的代码

  @interface KeyboardViewController (){
    NSMutableArray *_picHolderArray;

    }
    @property (nonatomic, strong) UIButton *nextKeyboardButton;
    @property (nonatomic, strong) UIScrollView *keyboardBackground;
@end

@implementation KeyboardViewController

- (void)updateViewConstraints {
    [super updateViewConstraints];
        // Add custom view sizing constraints here
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.keyboardBackground.delegate = self;
    // Perform custom UI setup here
    self.nextKeyboardButton = [UIButton buttonWithType:UIButtonTypeSystem];

    [self.nextKeyboardButton setTitle:NSLocalizedString(@"Next Keyboard", @"Title for 'Next Keyboard' button") forState:UIControlStateNormal];
    [self.nextKeyboardButton sizeToFit];
    self.nextKeyboardButton.translatesAutoresizingMaskIntoConstraints = NO;

    [self.nextKeyboardButton addTarget:self action:@selector(advanceToNextInputMode) forControlEvents:UIControlEventTouchUpInside];

    [self.view addSubview:self.nextKeyboardButton];

    NSLayoutConstraint *nextKeyboardButtonLeftSideConstraint = [NSLayoutConstraint constraintWithItem:self.nextKeyboardButton attribute:NSLayoutAttributeLeft relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeLeft multiplier:1.0 constant:0.0];
    NSLayoutConstraint *nextKeyboardButtonBottomConstraint = [NSLayoutConstraint constraintWithItem:self.nextKeyboardButton attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.view attribute:NSLayoutAttributeBottom multiplier:1.0 constant:0.0];
    [self.view addConstraints:@[nextKeyboardButtonLeftSideConstraint, nextKeyboardButtonBottomConstraint]];
    UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
    CGRect frame;

    if(UIDeviceOrientationIsLandscape(orientation))
        frame = CGRectMake(0, 0, 480, 162);
    else
        frame = CGRectMake(0, 0, 320, 216);

    self.keyboardBackground = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, 400, 180)];



    _picHolderArray = [[NSMutableArray alloc]init];
    UIImage* backgroundImage =[[UIImage imageNamed:@"keyBack.jpg"]
                               resizableImageWithCapInsets:UIEdgeInsetsMake(100, 0, 0, 0) ];
    UIGraphicsBeginImageContextWithOptions(self.keyboardBackground.bounds.size, NO, 0.0);
    [backgroundImage drawInRect:self.keyboardBackground.bounds];
    UIImage* newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    self.keyboardBackground.backgroundColor = [UIColor colorWithPatternImage:newImage];
    int _x = 0;
    for (int i = 0; i < 5; i++) {
        //load button size and pass in array for titles
        UIImageView *imageView  = [[UIImageView alloc]initWithFrame:CGRectMake(_x,45, 200, 120)];
        imageView.image = [UIImage imageNamed:@"testImage.jpg"];
        [imageView setClipsToBounds:YES];
        UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(singleTapGestureCaptured:)];
        [imageView addGestureRecognizer:singleTap];
        [imageView setMultipleTouchEnabled:YES];
        [imageView setUserInteractionEnabled:YES];

        // Create colored border using CALayer property
        [[imageView layer] setBorderColor:
         [[UIColor colorWithRed:(5.0/255.0) green:(108.0/255.0) blue:(182.0/255.0) alpha:1] CGColor]];
        [[imageView layer] setBorderWidth:1.75];
        [_picHolderArray addObject:imageView];

        [self.keyboardBackground addSubview:_picHolderArray[i]];
        _x += imageView.frame.size.width + 60;
    }

    //self.shell.showsHorizontalScrollIndicator = NO;
    self.keyboardBackground.autoresizesSubviews = YES;
   //self.keyboardBackground.pagingEnabled = YES;
    //self.keyboardBackground.scrollEnabled = YES;

    self.keyboardBackground.contentSize = CGSizeMake(_x, 180);


    [self.view addSubview:self.keyboardBackground];
}
- (void)singleTapGestureCaptured:(UITapGestureRecognizer *)gesture
{
    UIView *tappedView = [gesture.view hitTest:[gesture locationInView:gesture.view] withEvent:nil];
    NSLog(@"Touch event on view: %@",[tappedView class]);
}

/**********************************************************************
 *
 * scrollViewDidScroll: allows us to know which camera is viewed.
 *
 **********************************************************************/
#pragma mark - ScrollView Delegates
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    int  _index = ((int)(scrollView.contentOffset.x)/320);
    NSLog(@"%d",_index);
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView{
    NSLog(@"HEEERE");
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated
}

- (void)textWillChange:(id<UITextInput>)textInput {
    // The app is about to change the document's contents. Perform any preparation here.
}

- (void)textDidChange:(id<UITextInput>)textInput {
    // The app has just changed the document's contents, the document context has been updated.

    UIColor *textColor = nil;
    if (self.textDocumentProxy.keyboardAppearance == UIKeyboardAppearanceDark) {
        textColor = [UIColor whiteColor];
    } else {
        textColor = [UIColor blackColor];
    }
    [self.nextKeyboardButton setTitleColor:textColor forState:UIControlStateNormal];
}

@end

You can see NSLogs of your keyboard extension if you run that target. 如果运行目标,则可以看到键盘扩展的NSLogs。

You were most likely running your base app as your Xcode target. 您最有可能将基本应用程序作为Xcode目标运行。 Since your debugger is then attached to your base app rather than your extension, NSLogs will not be seen in the Console. 由于调试器随后将附加到基本应用程序而不是扩展程序,因此NSLogs将不会在控制台中显示。

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

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