1.
2.添加在当前控制器页面上 只在当前VC的view中显示可左右上下滑动 全局的话加在window上 直接上代码
- (UIButton *)factBtn{
if (!_factBtn) {
_factBtn = [UIButton buttonWithType:UIButtonTypeCustom];
_factBtn.frame = CGRectMake(kSWidth-65, 0.7*kSHeight, 40,40);
[_factBtn setImage:[UIImage imageNamed:@"baoliao"] forState:UIControlStateNormal];
[_factBtn addTarget:self action:@selector(factBtnClick:) forControlEvents:UIControlEventTouchUpInside];
UIPanGestureRecognizer *panGestureRecognizer = [[UIPanGestureRecognizer alloc]
initWithTarget:self
action:@selector(handlePan:)];
[_factBtn addGestureRecognizer:panGestureRecognizer];
}
return _factBtn;
}
- (void) handlePan:(UIPanGestureRecognizer*) recognizer
{
CGPoint translation = [recognizer translationInView:self.view];
CGFloat centerX=recognizer.view.center.x+ translation.x;
CGFloat thecenter=0;
recognizer.view.center=CGPointMake(centerX,
recognizer.view.center.y+ translation.y);
[recognizer setTranslation:CGPointZero inView:self.view];
if(recognizer.state==UIGestureRecognizerStateEnded|| recognizer.state==UIGestureRecognizerStateCancelled) {
if(centerX>kSWidth/2) {
thecenter=kSWidth-40/2;
}else{
thecenter=40/2;
}
// contentOff_Y button允许拖动的Y值 根据需求调整(button没有Y值限制时删除下面两个判断即可)
// Height_TabBar: tabbar高度 Height_NavBar: navbar 高度
CGFloat contentOff_Y = recognizer.view.center.y;
if (contentOff_Y > kSHeight-Height_TabBar-40) {
contentOff_Y = kSHeight-Height_TabBar-20;
}
if (contentOff_Y < Height_NavBar) {
contentOff_Y = Height_NavBar+20;
}
[UIView animateWithDuration:0.3 animations:^{
recognizer.view.center=CGPointMake(thecenter,
contentOff_Y+ translation.y);
}];
}
}