您的位置:首页 > 移动开发 > IOS开发

ios子视图和父视图同时处理输入事件

2015-07-06 16:42 543 查看
在使用UITableView嵌套UICollectionView的过程中,

collectionView作为cell,我希望,点击collectionView的让tableView对应的行也被选中。

如果直接判断collectionView所在的行,然后设置cell.selected = YES,选中是解决了,但是,

选择其他的cell的以后,上一个cell的selected的状态却没有变成NO,就这不对了。如果我们自己

处理这个取消选中状态的操作,也不是什么麻烦的事情。因为是单选,我们只需记住当前选中的cell,

但这个cell要改变时,先取消其选中状态,在将它指向当前选择的cell。

当然,我还可以换一个方法。在collectionView接受用户触摸的时候,tableView也接受该触摸。

各自处理各自的。

在自定义的collectionView子类中重写touchBegan、touchMove、touchEnd、touchCancel这四个方法。
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{

[self.nextResponder touchesBegan:touches withEvent:event];
[super touchesBegan:touches withEvent:event];
}

-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.nextResponder touchesMoved:touches withEvent:event];
[super touchesMoved:touches withEvent:event];
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.nextResponder touchesEnded:touches withEvent:event];
[super touchesEnded:touches withEvent:event];
}

-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.nextResponder touchesCancelled:touches withEvent:event];
[super touchesCancelled:touches withEvent:event];
}


可以看到,collectionView的nextResponder是UITableViewCellContentView。

< BookCollectionView.m:(78) > <UITableViewCellContentView: 0x7fe25b84a6d0; frame = (0 0; 320 151.5); opaque = NO; gestureRecognizers = <NSArray: 0x7fe259e578a0>; layer = <CALayer: 0x7fe25b8492d0>>

注意,touch系列的方法需要全部实现,仅仅实现touchBegan会出现一些奇怪的问题。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  ios nextResponder