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

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回答

1482閲覧

swift AppDeldateについて

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/26 09:43

編集2016/04/27 08:26

ボタンをクラス1で作り、タップ機能があり
クラス2ではスワイプしたらの機能を作っていて
スワイプし終わり、ある場所以外は元の位置に戻る・・・

と言うものを作っていて、クラスが別なので
クラス2の方ではボタンを宣言してないので波線が出てきます。

そこでボタンをデリゲートに入れたのですが、エラーがでます。

コードは・・・
class save: UIViewController{
var hozon = CustomButton(type: .Custom)
var add:AppDelegate = UIApplication.sharedApplication().delegate as! AppDelegate

add.hozon.frame = (frame: CGRectMake(145,290,250,250))
add.hozon.setBackgroundImage(UIImage(named: "Chiba paper1.png"),forState: UIControlState.Normal)
add.hozon.addTarget(self, action: #selector(save.buttonPressed(_:)), forControlEvents:.TouchUpInside)
add.hozon.userInteractionEnabled = true
add.hozon.tag = 1
add.hozon.removeRect = Trash_can.frame
view.addSubview(hozon)

func buttonPressed(sender: UIButton) {
if !add.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{

// add.hozon.frame.origin.y = 910
// add.hozon.frame.origin.y = 150
}

isMoveing = false if position == self.frame.origin { self.sendActionsForControlEvents(.TouchUpInside)} }

を使って作成しています。

デリゲートではなくても
他に良い方法がありましたら
どうか教えてください、お願い致します。

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

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

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

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

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

_Kentarou

2016/04/26 15:32 編集

実装方法が文章からは読み取れないので、コードを載せて頂いたほうが適切なアドバイスができると思います。
guest

回答1

0

ベストアンサー

コードを見ても、やりたい仕様が理解できませんでした、、、
おそらくこのような事をやりたいのかなぁ、位でとりあえず作ってみました。

コードが何をやっているのか理解しないとアレンジもできないと思いますので、どういう動きをしているのかじっくり見て、考える事が必要だと思います。

swift

1import UIKit 2 3class ViewController: UIViewController, CustomButtonDelegate { 4 5 let genetateButton = CustomButton(type: .Custom) 6 var buttonArray: [CustomButton] = [] 7 var buttonTag: Int = 1 8 9 let removeZoneView = UIView(frame: CGRectMake(0, 0, 200, 200)) 10 11 override func viewDidLoad() { 12 super.viewDidLoad() 13 14 removeZoneView.backgroundColor = UIColor.redColor() 15 view.addSubview(removeZoneView) 16 17 // ボタンを生成 18 genetateButton.frame = CGRectMake(100, 300, 100, 50) 19 genetateButton.setTitle("Button", forState: .Normal) 20 genetateButton.setTitleColor(UIColor.blueColor(), forState: .Normal) 21 genetateButton.backgroundColor = UIColor.yellowColor() 22 genetateButton.addTarget(self, action: #selector(ViewController.buttonPressed(_:)), forControlEvents: .TouchUpInside) 23 genetateButton.userInteractionEnabled = true 24 genetateButton.tag = 0 25 // 消したい場所(frame)を設定 26 //genetateButton.removeRect = removeZoneView.frame 27 view.addSubview(genetateButton) 28 } 29 30 func buttonPressed(sender: CustomButton) { 31 if !sender.isMoveing && sender.tag == 0 { 32 // ボタン押下時のイベント 33 addButton() 34 print("AddButton") 35 return 36 } 37 38 if !sender.isMoveing { 39 print("Push ButtonTag = \(sender.tag)") 40 } 41 } 42 43 44 func addButton() { 45 // ボタンを生成 46 let button = CustomButton(type: .Custom) 47 button.frame = CGRectMake(100, 400, 100, 50) 48 button.setTitle("Button" + "\(buttonTag)", forState: .Normal) 49 button.setTitleColor(UIColor.blueColor(), forState: .Normal) 50 button.backgroundColor = UIColor.greenColor() 51 button.addTarget(self, action: #selector(ViewController.buttonPressed(_:)), forControlEvents: .TouchUpInside) 52 button.userInteractionEnabled = true 53 button.tag = buttonTag 54 button.delegate = self 55 button.removeRect = removeZoneView.frame 56 view.addSubview(button) 57 buttonArray.append(button) 58 59 buttonTag += 1 60 } 61 62 func removeButton(button: CustomButton) { 63 if let num = buttonArray.indexOf(button) { 64 buttonArray.removeAtIndex(num) 65 } 66 } 67} 68 69protocol CustomButtonDelegate: class { 70 func removeButton(button: CustomButton) 71} 72 73 74// CustomButton Class 75class CustomButton: UIButton { 76 77 weak var delegate: CustomButtonDelegate! 78 var isMoveing : Bool = false 79 var removeRect: CGRect! 80 var position : CGPoint! 81 82 83 override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 84 super.touchesBegan(touches, withEvent: event) 85 position = self.frame.origin 86 } 87 88 override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) { 89 super.touchesMoved(touches, withEvent: event) 90 91 isMoveing = true 92 93 let touchEvent = touches.first! 94 95 // ドラッグ前の座標 96 let preDx = touchEvent.previousLocationInView(superview).x 97 let preDy = touchEvent.previousLocationInView(superview).y 98 99 // ドラッグ後の座標 100 let newDx = touchEvent.locationInView(superview).x 101 let newDy = touchEvent.locationInView(superview).y 102 103 // ドラッグしたx座標の移動距離 104 let dx = newDx - preDx 105 106 // ドラッグしたy座標の移動距離 107 let dy = newDy - preDy 108 109 // 画像のフレーム 110 var viewFrame: CGRect = self.frame 111 112 // 移動分を反映させる 113 viewFrame.origin.x += dx 114 viewFrame.origin.y += dy 115 self.frame = viewFrame 116 } 117 118 override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { 119 super.touchesEnded(touches, withEvent: event) 120 121 if let rect = removeRect where CGRectIntersectsRect(self.frame, rect) { 122 // 消したいFrameと重なっている場合 123 124 delegate?.removeButton(self) 125 self.removeFromSuperview() 126 } else { 127 128 // コメントを外すとボタンを元の位置に戻します 129// UIView.animateWithDuration(0.3, animations: { 130// self.frame.origin = self.position 131// }) 132 } 133 134 isMoveing = false 135 if position == self.frame.origin { 136 self.sendActionsForControlEvents(.TouchUpInside) 137 } 138 } 139}

投稿2016/04/27 22:42

_Kentarou

総合スコア8490

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

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

dynamite_nakaji

2016/04/28 07:26

説明不足ですみません。 私がやりたいことはボタンを動かしてImage.view以外なら元の位置に戻したいわけです。 しかしクラス2でスワイプの操作をしているのでtouchEndの時にボタンの座標を 宣言出来なくて困っているのです!! そこでデリゲートにチャレンジしましたが 出来ませんでした。他の方法に良いものがあったら教えてください。
_Kentarou

2016/04/28 07:37

コメントアウトしている部分を外すと戻る動きにはできますが、それでは問題ありますか?
dynamite_nakaji

2016/04/29 00:52

あ!!すっかり見落としていました・・・ 少し問題がありますが自分でどうにかしてみることにします 毎度毎度ありがとうございます
dynamite_nakaji

2016/04/29 00:59

やっぱり私では難しかったです スワイプし終わってもsubviewが出てしまうのを止めたいと 考えています・・・しかしどうすればいいのか
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問