iOS using UITapGestureRecognizer with UILabel to s

2019-06-14 12:10发布

Sorry if I overlooked a potential answer to my question, but I'm having trouble with using the UITapGestureRecognizer with a UILabel inside a subview UIViewController class...

Basically, I've created a custom UIViewController that has a UILabel and a few other irrelevant elements. This custom UIViewController, however, is inside a custom UIScrollView with paging enabled and the labels are offset just enough so you can see the next UIViewController's label. What I want to do is when the user touches the "next" label, the scrollRectToVisible:animated: method fires, essentially switching pages without scrolling. The CustomViewController's UILabels are situated on the top.

Here's an example code for the container UIScrollView when adding the UITapGestureRecognizer to the CustomViewController's UILabel:

[scrollView addSubview:CustomViewController];

- (void) addSubview: (CustomViewController *) view {
    // create view's frame here...
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fireScrollRectToVisible:view)]; // this is the current problem like a lot of people out there...
    [view.Label addGestureRecognizer:tap];
    // [super addSubview:view.view];
}
- (void) fireScrollRectToVisible: (CustomViewController *) cvc {
    CGRect frame = CGRectMake(cvc.view.frame.origin.x, cvc.view.frame.origin.y, 320, 480);
    [scrollView scrollRectToVisible: frame animated:YES];
}

I initially thought this would be easy, but since the @selector doesn't allow you to place arguments it made it incredibly difficult. I think what I need is to access the CustomViewController's frame and set the scrollRectToVisible to that, but I'm not sure anymore...

I've tried this post but I'm very new at Objective-C and I don't fully understand the hitTest:withEvent: I'm assuming the hitTest:(CGPoint) has something to do with the view's bounds?

UITapGestureRecognizer initWithTarget:action: method to take arguments?

If anyone can point me in the right direction that would be great. Any help would be greatly appreciated!

2条回答
Explosion°爆炸
2楼-- · 2019-06-14 12:33

If you need the frame of the view that contains the label that contains the gesture recognizer, couldn't you just do:

gestureRecognizer.view.superview.frame;

to get the frame?

The selector for your gesture recognizer should be of the form

- (void)gestureRecognizerDidFire:(UIGestureRecognizer *)recognizer;

the recognizer will get passed automatically if you set the selector to @selector(gestureRecognizerDidFire:) and you can access the view property therein.

查看更多
【Aperson】
3楼-- · 2019-06-14 12:38

I do some change based on your code

- (void) addSubview: (CustomViewController *) view {
    // create view's frame here...
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(fireScrollRectToVisible:)]; // this is the current problem like a lot of people out there...
    [view.Label addGestureRecognizer:tap];
    // [super addSubview:view.view];
}
- (void) fireScrollRectToVisible: (UIGestureRecognizer *) gesture {
    UIView *view = [gesture.view superview];
    CGRect frame = CGRectMake(view.frame.origin.x, view.frame.origin.y, 320, 480);
    [scrollView scrollRectToVisible: frame animated:YES];
}

hope this can help you.

查看更多
登录 后发表回答