1.
ex_one.gif
ex_one.gif
2.添加在當(dāng)前控制器頁面上 只在當(dāng)前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值 根據(jù)需求調(diào)整(button沒有Y值限制時(shí)刪除下面兩個(gè)判斷即可)
// 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);
}];
}
}