最近產品抽風,想做許鮮網的那個小客服按鈕,雖然沒啥難度,但是我懶啊,哈哈,上度娘搞了一個,但是點擊事件和拖動重復了,擦。干脆寫一個吧,僅供參考。
話不多說,上代碼:
- (UIButton *)panButton { if (!_panButton) { UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)]; _panButton = [[UIButton alloc] init]; _panButton.backgroundColor = [UIColor blueColor]; _panButton.layer.borderWidth = 1.f; _panButton.layer.borderColor = [UIColor greenColor].CGColor; [_panButton setTitle:@"清除緩存" forState:UIControlStateNormal]; _panButton.titleLabel.font = [UIFont systemFontOfSize:9]; [_panButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside]; [_panButton addGestureRecognizer:panGesture]; } return _panButton;}- (void)panAction:(UIPanGestureRecognizer *)recognizer { CGPoint translationPoint = [recognizer translationInView:self.view]; CGPoint center = recognizer.view.center; recognizer.view.center = CGPointMake(center.x + translationPoint.x, center.y + translationPoint.y); [recognizer setTranslation:CGPointZero inView:self.view];}-(void)buttonAction:(UIButton *)sender{ NSLog(@"煩人,點我干啥~");}
以上這篇ios可拖動按鈕實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持VEVB武林網。
新聞熱點
疑難解答