demo效果
这个效果比较简单,直接记录一下。
自定义一个继承自UIView的视图,定义两个方法一个显示方法,一个消失方法。
/**
* 显示属性选择视图
*
* @param view 要在哪个视图中显示
*/
- (void)showInView:(UIView *)view;
/**
* 属性视图的消失
*/
- (void)removeView;
这两个方法的实现:
/**
* 显示属性选择视图
*
* @param view 要在哪个视图中显示
*/
- (void)showInView:(UIView *)view {
[view addSubview:self];
__weak typeof(self) _weakSelf = self;
self.contentView.frame = CGRectMake(0, kHeight, kWidth, kATTR_VIEW_HEIGHT);;
[UIView animateWithDuration:0.3 animations:^{
_weakSelf.backgroundColor = [UIColor colorWithWhite:0 alpha:0.5];
_weakSelf.contentView.frame = CGRectMake(0, kHeight - kATTR_VIEW_HEIGHT, kWidth, kATTR_VIEW_HEIGHT);
}];
}
/**
* 属性视图的消失
*/
- (void)removeView {
__weak typeof(self) _weakSelf = self;
[UIView animateWithDuration:0.3 animations:^{
_weakSelf.backgroundColor = [UIColor clearColor];
_weakSelf.contentView.frame = CGRectMake(0, kHeight, kWidth, kATTR_VIEW_HEIGHT);
} completion:^(BOOL finished) {
[_weakSelf removeFromSuperview];
}];
}
然后在这个自定义视图的初始化方法中添加手势,点击调用视图消失的方法。
// 添加手势,点击背景视图消失
UITapGestureRecognizer *tapBackGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(removeView)];
tapBackGesture.delegate = self;
[self addGestureRecognizer:tapBackGesture];
我们要初始化一个内容视图作为基准,点击内容视图之外的地方作用手势,视图之内的不作用手势,要实现手势的代理方法。
#pragma mark - UIGestureRecognizerDelegate
//确定点击范围
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
if ([touch.view isDescendantOfView:self.contentView]) {
return NO;
}
return YES;
}