swift AppDeldateについて
- 評価
- クリップ 0
- VIEW 1,026
ボタンをクラス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)}
}
を使って作成しています。
デリゲートではなくても
他に良い方法がありましたら
どうか教えてください、お願い致します。
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 過去に投稿した質問と同じ内容の質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
checkベストアンサー
0
コードを見ても、やりたい仕様が理解できませんでした、、、
おそらくこのような事をやりたいのかなぁ、位でとりあえず作ってみました。
コードが何をやっているのか理解しないとアレンジもできないと思いますので、どういう動きをしているのかじっくり見て、考える事が必要だと思います。
import UIKit
class ViewController: UIViewController, CustomButtonDelegate {
let genetateButton = CustomButton(type: .Custom)
var buttonArray: [CustomButton] = []
var buttonTag: Int = 1
let removeZoneView = UIView(frame: CGRectMake(0, 0, 200, 200))
override func viewDidLoad() {
super.viewDidLoad()
removeZoneView.backgroundColor = UIColor.redColor()
view.addSubview(removeZoneView)
// ボタンを生成
genetateButton.frame = CGRectMake(100, 300, 100, 50)
genetateButton.setTitle("Button", forState: .Normal)
genetateButton.setTitleColor(UIColor.blueColor(), forState: .Normal)
genetateButton.backgroundColor = UIColor.yellowColor()
genetateButton.addTarget(self, action: #selector(ViewController.buttonPressed(_:)), forControlEvents: .TouchUpInside)
genetateButton.userInteractionEnabled = true
genetateButton.tag = 0
// 消したい場所(frame)を設定
//genetateButton.removeRect = removeZoneView.frame
view.addSubview(genetateButton)
}
func buttonPressed(sender: CustomButton) {
if !sender.isMoveing && sender.tag == 0 {
// ボタン押下時のイベント
addButton()
print("AddButton")
return
}
if !sender.isMoveing {
print("Push ButtonTag = \(sender.tag)")
}
}
func addButton() {
// ボタンを生成
let button = CustomButton(type: .Custom)
button.frame = CGRectMake(100, 400, 100, 50)
button.setTitle("Button" + "\(buttonTag)", forState: .Normal)
button.setTitleColor(UIColor.blueColor(), forState: .Normal)
button.backgroundColor = UIColor.greenColor()
button.addTarget(self, action: #selector(ViewController.buttonPressed(_:)), forControlEvents: .TouchUpInside)
button.userInteractionEnabled = true
button.tag = buttonTag
button.delegate = self
button.removeRect = removeZoneView.frame
view.addSubview(button)
buttonArray.append(button)
buttonTag += 1
}
func removeButton(button: CustomButton) {
if let num = buttonArray.indexOf(button) {
buttonArray.removeAtIndex(num)
}
}
}
protocol CustomButtonDelegate: class {
func removeButton(button: CustomButton)
}
// CustomButton Class
class CustomButton: UIButton {
weak var delegate: CustomButtonDelegate!
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) {
// 消したいFrameと重なっている場合
delegate?.removeButton(self)
self.removeFromSuperview()
} else {
// コメントを外すとボタンを元の位置に戻します
// UIView.animateWithDuration(0.3, animations: {
// self.frame.origin = self.position
// })
}
isMoveing = false
if position == self.frame.origin {
self.sendActionsForControlEvents(.TouchUpInside)
}
}
}
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 88.37%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる
質問への追記・修正、ベストアンサー選択の依頼
_Kentarou
2016/04/26 22:35 編集
実装方法が文章からは読み取れないので、コードを載せて頂いたほうが適切なアドバイスができると思います。