本文主要是介绍XMG 抽屉效果,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
1.比如说我创建了3个View
-(void)viewDidLoad{
[ super viewDidLoad];
[self setUpChild] ;
UIPanGestureRecognizer *pan=[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
[ self.view addGestureRecognizer:pan]
// Observer:观察者 谁想监听
// KeyPath:监听的属性
// options:监听新值的改变
[_mainV addObserver:self forKeyPath:@"frame" options:NSKeyValueObservingOptionNew context:nil];
}
-
//如果想要监听一个对象的某个属性可以使用KVO
// 只要监听的属性一改变,就会调用观察者的这个方法,通知你有新值
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
NSLog(@"%@",NSStringFromCGRect(_mainV.frame));
if (_mainV.frame.origin.x > 0) { // 往右边移动,隐藏蓝色的view
_rightV.hidden = YES;
}else if (_mainV.frame.origin.x < 0){ // 往左边移动,显示蓝色的view
_rightV.hidden = NO;
}
使用KVO之后一定要记得在dealloc方法中移除监听.当对象被销毁的时候移除观察者
-(void)dealloc{
[_main removeObserve:self keyPath:@"frame"];
}
//创建对应的View
- (void)setUpChildView
{
// left
UIView *leftV = [[UIView alloc] initWithFrame:self.view.bounds];
leftV.backgroundColor = [UIColor greenColor];
[self.view addSubview:leftV];
_leftV = leftV;
// right
UIView *rightV = [[UIView alloc] initWithFrame:self.view.bounds];
rightV.backgroundColor = [UIColor blueColor];
[self.view addSubview:rightV];
_rightV = rightV;
// main
UIView *mainV = [[UIView alloc] initWithFrame:self.view.bounds];
mainV.backgroundColor = [UIColor redColor];
[self.view addSubview:mainV];
_mainV = mainV;
}
这篇关于XMG 抽屉效果的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!