开发者

iPhone Dev: Making a tableHeaderView respond to touches

开发者 https://www.devze.com 2022-12-09 13:23 出处:网络
Ok, I\'m a objective c noob. I have a table view with a tableHeaderView and I 开发者_如何学Cwant it to respond to touches?

Ok, I'm a objective c noob. I have a table view with a tableHeaderView and I 开发者_如何学Cwant it to respond to touches?

Here are some steps that I do when creating the view: 1. create a view 2. add a bunch of labels and imageviews to it 3. set my tableHeaderView to the view I created

Any help will be appreciated!


UITapGestureRecognizer *recognizer = [[UITapGestureRecognizer alloc]
                                      initWithTarget:self
                                      action:@selector(tapTableHeader:)];
[self.tableView.tableHeaderView addGestureRecognizer:recognizer];

- (void)tapTableHeader:(UITapGestureRecognizer *)recognizer {
    NSLog(@"table header taped");
}


Is the desired touch behavior especially complex? If not, just make the part of the header that needs to respond to touches a button, slider, switch or other control.


The header view can respond to touches the same way any other view can. You can put buttons on it or you can override the UIResponder methods that handle touch events, specifically:

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event;


add a bunch of labels and imageviews to it

Might want to be careful about that from a user interface design perspective. The effective touch area of a table header is rather small. You don't want it to have more than one or at most two components or possible actions. Otherwise, your users will need a stylus to do anything.

If you just need an image/text and to respond to taps, consider using a UIButton. It will provide you with all that functionality without subclassing.

0

精彩评论

暂无评论...
验证码 换一张
取 消