swift 拖动按钮_swift -> 给任意控件添加 手势(点击,拖拽等) 事件 - 大风 - ITeye博客...

1、拍击UITapGestureRecognizer (任意次数的拍击)      2、向里或向外捏UIPinchGestureRecognizer (用于缩放)      3、摇动或者拖拽UIPanGestureRecognizer      4、擦碰UISwipeGestureRecognizer (一般用以左右切换滑动)      5、旋转UIRotationGestureRecognizer (手指朝相反方向移动)      6、长按UILongPressGestureRecognizer

override func viewDidLoad() {

super.viewDidLoad()

var v = UIView(frame: CGRect(x: 0, y: 0, width: screenWidth, height: screenHeight));

//点击事件

let handTap = UITapGestureRecognizer(target: self, action: #selector(funTap))

v.addGestureRecognizer(handTap)

//拖动事件, 任意方向

let handDrag = UIPanGestureRecognizer(target: self, action: #selector(funDrag))

//v.addGestureRecognizer(handDrag)

//左右滑动,不可与拖动事件UIPanGestureRecognizer并存 , 默认只支持向右

let handLeftRight = UISwipeGestureRecognizer(target: self, action: #selector(funLeftRight))

handLeftRight.direction = .left //支持向左

v.addGestureRecognizer(handLeftRight)

self.view.addSubview(v)

}

func funTap(sender: UIPanGestureRecognizer){

print("funTap_"+String(arc4random()));

}

func funDrag(sender: UIPanGestureRecognizer){

var Point = sender.translation(in: self.view);//现对于起始点的移动位置

Point = sender.location(in: self.view);//在整个self.view 中的位置

print("funDrag_"+String(describing: Point.x)+","+String(describing:Point.y))

if(sender.state == .began){

print("begin: "+String(describing: Point.x)+","+String(describing:Point.y))

}else if(sender.state == .ended){

print("ended: "+String(describing: Point.x)+","+String(describing:Point.y))

}else{

print("ing: "+String(describing: Point.x)+","+String(describing:Point.y))

}

}

func funLeftRight(sender: UIPanGestureRecognizer){

print("funLeftRight_"+String(arc4random()));

}

如果想获取 参数 sender:UIxxxGestureRecognizer 中的视图 信息

func showLongPress(sender:UILongPressGestureRecognizer){

print((sender.view as! XXX).url.text);

}

** 传递 和 获取 UIButton 信息

func xxx(sender: UILongPressGestureRecognizer){

let btn:UIButton = sender.view as! UIButton

print(btn.tag)

}

** 其中 UILongPressGestureRecogizer  长按事件详解

class xxx: aaa, UIGestureRecognizerDelegate{

//让 添加 的 长按事件 与 控件本身 的长按事件 并存,不添加不会触发 addGestureRecognizer 的长按

func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldRecognizeSimultaneouslyWith otherGestureRecognizer: UIGestureRecognizer) -> Bool {

return true;

}

func longPressWeb(sender:UILongPressGestureRecognizer){

if(sender.state == .began){

print("long preses began")

}else if(sender.state == .ended){

print(" long press end")

}

print("you do long press");

}

override func viewDidLoad() {

let long = UILongPressGestureRecognizer(target: self, action: #selector(longPressWeb));

long.delegate = self

self.view.addGestureRecognizer(long)

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值