class save: UIViewController{
var hozon = CustomButton(type: .Custom)
hozon.frame = (frame: CGRectMake(145,290,250,250))
hozon.setBackgroundImage(UIImage(named: "Chiba paper1.png"),forState: UIControlState.Normal)
hozon.addTarget(self, action: #selector(save.buttonPressed(_:)), forControlEvents:.TouchUpInside)
hozon.userInteractionEnabled = true
hozon.tag = 1
hozon.removeRect = Trash_can.frame
view.addSubview(hozon)
func buttonPressed(sender: UIButton) {
if !hozon.isMoveing && sender.tag == 1 {
view.addSubview(configuration)
}
class CustomButton: UIButton {
var isMoveing : Bool = false
var removeRect: CGRect!
var position : CGPoint!
override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { super.touchesBegan(touches, withEvent: event) position = self.frame.origin } override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { super.touchesMoved(touches, withEvent: event) isMoveing = true let touchEvent = touches.first! // ドラッグ前の座標 let preDx = touchEvent.previousLocationInView(superview).x let preDy = touchEvent.previousLocationInView(superview).y // ドラッグ後の座標 let newDx = touchEvent.locationInView(superview).x let newDy = touchEvent.locationInView(superview).y // ドラッグしたx座標の移動距離 let dx = newDx - preDx // ドラッグしたy座標の移動距離 let dy = newDy - preDy // 画像のフレーム var viewFrame: CGRect = self.frame // 移動分を反映させる viewFrame.origin.x += dx viewFrame.origin.y += dy self.frame = viewFrame } override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { super.touchesEnded(touches, withEvent: event) if let rect = removeRect where CGRectIntersectsRect(self.frame, rect) { self.removeFromSuperview() }else{
コメントを外すとボタンを元の位置に戻します
UIView.animateWithDuration(0.3, animations: {
self.frame.origin = self.position})
}
isMoveing = false
if position == self.frame.origin {
self.sendActionsForControlEvents(.TouchUpInside)}
}
このコードを使っています。誰かのコードの引用ですが・・・
これを使っているとスワイプし終わって元の位置に行ったらサブビューが出てきます
私はタップした時のみサブビューが出るようにしたいのですが、スワイプして消えなかった以外
出てきてしまうのでどうにかしてほしいです。どうかよろしくお願い致します!!
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2016/04/30 01:01