Swift-拖动按钮从一个位置到另一个位置
发布时间:2020-12-14 02:25:58 所属栏目:百科 来源:网络整理
导读:我正在尝试使用UITouch将按钮从一个位置拖动到另一个位置.但是我无法拖动它.我在添加按钮目标时遇到问题… 我的代码 – override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view,typically from a nib. let b
我正在尝试使用UITouch将按钮从一个位置拖动到另一个位置.但是我无法拖动它.我在添加按钮目标时遇到问题…
我的代码 – override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view,typically from a nib. let btn_swap = UIButton.buttonWithType(UIButtonType.Custom) as UIButton! btn_swap .setTitle("Drag Me",forState: UIControlState.Normal) btn_swap.backgroundColor = UIColor.yellowColor() btn_swap.addTarget(self,action: "wasDragged:",forControlEvents: UIControlEvents.TouchDragInside) btn_swap.frame = CGRectMake((self.view.bounds.size.width - 100)/2.0,(self.view.bounds.size.height - 50)/2.0,100,50) self.view.addSubview(btn_swap) self.creation_of_btn() } func wasDragged (buttn : UIButton,event :UIEvent) { var touch : UITouch = event.touchesForView(buttn) . anyObject() as UITouch var previousLocation : CGPoint = touch .previousLocationInView(buttn) var location : CGPoint = touch .locationInView(buttn) var delta_x :CGFloat = location.x - previousLocation.x var delta_y :CGFloat = location.y - previousLocation.y buttn.center = CGPointMake(buttn.center.x + delta_x,buttn.center.y + delta_y); }
你给按钮选择了一个错误的选择器.Dragged:.因为你的动作方法看起来像
func wasDragged (buttn : UIButton,event :UIEvent) { } slector应该是wasDragged:event:. btn_swap.addTarget(self,action: "wasDragged:event:",forControlEvents: UIControlEvents.TouchDragInside) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |