ios可拖动按钮实例
最近产品抽风,想做许鲜网的那个小客服按钮,虽然没啥难度,但是我懒啊,哈哈,上度娘搞了一个,但是点击事件和拖动重复了,擦。干脆写一个吧,仅供参考。
话不多说,上代码:
-(UIButton*)panButton{
if(!_panButton){
UIPanGestureRecognizer*panGesture=[[UIPanGestureRecognizeralloc]initWithTarget:selfaction:@selector(panAction:)];
_panButton=[[UIButtonalloc]init];
_panButton.backgroundColor=[UIColorblueColor];
_panButton.layer.borderWidth=1.f;
_panButton.layer.borderColor=[UIColorgreenColor].CGColor;
[_panButtonsetTitle:@"清除缓存"forState:UIControlStateNormal];
_panButton.titleLabel.font=[UIFontsystemFontOfSize:9];
[_panButtonaddTarget:selfaction:@selector(buttonAction:)forControlEvents:UIControlEventTouchUpInside];
[_panButtonaddGestureRecognizer:panGesture];
}
return_panButton;
}
-(void)panAction:(UIPanGestureRecognizer*)recognizer{
CGPointtranslationPoint=[recognizertranslationInView:self.view];
CGPointcenter=recognizer.view.center;
recognizer.view.center=CGPointMake(center.x+translationPoint.x,center.y+translationPoint.y);
[recognizersetTranslation:CGPointZeroinView:self.view];
}
-(void)buttonAction:(UIButton*)sender
{
NSLog(@"烦人,点我干啥~");
}
以上这篇ios可拖动按钮实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。