質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Q&A

解決済

1回答

2188閲覧

swift スワイプ・・・

dynamite_nakaji

総合スコア42

iOS

iOSとは、Apple製のスマートフォンであるiPhoneやタブレット端末のiPadに搭載しているオペレーションシステム(OS)です。その他にもiPod touch・Apple TVにも搭載されています。

Xcode

Xcodeはソフトウェア開発のための、Appleの統合開発環境です。Mac OSXに付随するかたちで配布されています。

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

0グッド

0クリップ

投稿2016/04/29 15:16

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)}
}

このコードを使っています。誰かのコードの引用ですが・・・
これを使っているとスワイプし終わって元の位置に行ったらサブビューが出てきます
私はタップした時のみサブビューが出るようにしたいのですが、スワイプして消えなかった以外
出てきてしまうのでどうにかしてほしいです。どうかよろしくお願い致します!!

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

CustomButtonクラスの以下の部分のみ記述する位置を変更しました。

swift

1isMoveing = false 2if position == self.frame.origin { 3 self.sendActionsForControlEvents(.TouchUpInside) 4}

CustomButtonクラス

swift

1// CustomButton Class 2class CustomButton: UIButton { 3 4 weak var delegate: CustomButtonDelegate! 5 var isMoveing : Bool = false 6 var removeRect: CGRect! 7 var position : CGPoint! 8 9 10 override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 11 super.touchesBegan(touches, withEvent: event) 12 position = self.frame.origin 13 } 14 15 override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { 16 super.touchesMoved(touches, withEvent: event) 17 18 isMoveing = true 19 20 let touchEvent = touches.first! 21 22 // ドラッグ前の座標 23 let preDx = touchEvent.previousLocationInView(superview).x 24 let preDy = touchEvent.previousLocationInView(superview).y 25 26 // ドラッグ後の座標 27 let newDx = touchEvent.locationInView(superview).x 28 let newDy = touchEvent.locationInView(superview).y 29 30 // ドラッグしたx座標の移動距離 31 let dx = newDx - preDx 32 33 // ドラッグしたy座標の移動距離 34 let dy = newDy - preDy 35 36 // 画像のフレーム 37 var viewFrame: CGRect = self.frame 38 39 // 移動分を反映させる 40 viewFrame.origin.x += dx 41 viewFrame.origin.y += dy 42 self.frame = viewFrame 43 } 44 45 override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { 46 super.touchesEnded(touches, withEvent: event) 47 48 if let rect = removeRect where CGRectIntersectsRect(self.frame, rect) { 49 // 消したいFrameと重なっている場合 50 51 delegate?.removeButton(self) 52 self.removeFromSuperview() 53 54 } else { 55 56 isMoveing = false 57 if position == self.frame.origin { 58 self.sendActionsForControlEvents(.TouchUpInside) 59 } 60 61 // ボタンを元の位置に戻す 62 UIView.animateWithDuration(0.3, animations: { 63 self.frame.origin = self.position 64 }) 65 } 66 } 67}

投稿2016/04/29 15:33

_Kentarou

総合スコア8490

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

dynamite_nakaji

2016/04/30 01:01

な、なるほど〜 さすがですね!!ありがとうございます^ ^
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問