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

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

2219閲覧

swift 選択した色をタッチしたUIImageViewに反映させたい。

nobuno

総合スコア10

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クリップ

投稿2015/11/03 15:54

画面下に赤青緑の3色があり、画面上には16個の UIImageViewがあります。

自分がやりたいのは下の3色のどれかをタッチした後に上のUIImageViewをタッチするとその色が反映されるコードを書きたいです。

例えば、下の3つある選択肢から赤を選んだ場合、赤をタッチした後に上のマス目で区切られている UIImageViewのうちどれか一つをタッチするとそのタッチした UIImageViewの背景を赤にするといった感じです。
青でも緑でも同様です。

質問がわかりにくくて申し訳有りません。

コードを書いていただけると助かります。

下にコードを載せておきます。

UIImageView〜 UIImageView16までが色が反映されるところです。
UIImageViewA,B,Cは下の3つの選択肢です。

ご回答お待ちしております。

class ViewController: UIViewController {

private var myImageView: UIImageView! private var myImageView2: UIImageView! private var myImageView3: UIImageView! private var myImageView4: UIImageView! private var myImageView5: UIImageView! private var myImageView6: UIImageView! private var myImageView7: UIImageView! private var myImageView8: UIImageView! private var myImageView9: UIImageView! private var myImageView10: UIImageView! private var myImageView11: UIImageView! private var myImageView12: UIImageView! private var myImageView13: UIImageView! private var myImageView14: UIImageView! private var myImageView15: UIImageView! private var myImageView16: UIImageView! private var myImageView17: UIImageView! private var myImageView18: UIImageView! private var myImageView19: UIImageView! private var myImageView20: UIImageView! private var myImageView21: UIImageView! private var myImageView22: UIImageView! private var myImageViewA: UIImageView! private var myImageViewB: UIImageView! private var myImageViewC: UIImageView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. myImageView = UIImageView(frame: CGRectMake(0,0,30,30)) myImageView.layer.position = CGPoint(x: self.view.bounds.width/6, y: self.view.bounds.height/4.4) self.view.addSubview(myImageView) myImageView2 = UIImageView(frame: CGRectMake(0,0,30,30)) myImageView2.layer.position = CGPoint(x: self.view.bounds.width/3.8, y: self.view.bounds.height/4.4) self.view.addSubview(myImageView2) myImageView3 = UIImageView(frame: CGRectMake(0,0,30,30)) myImageView3.layer.position = CGPoint(x: self.view.bounds.width/2.8, y: self.view.bounds.height/4.4) self.view.addSubview(myImageView3) myImageView4 = UIImageView(frame: CGRectMake(0,0,30,30)) myImageView4.layer.position = CGPoint(x: self.view.bounds.width/2.22, y: self.view.bounds.height/4.4) self.view.addSubview(myImageView4) myImageView5 = UIImageView(frame: CGRectMake(0,0,30,30)) myImageView5.layer.position = CGPoint(x: self.view.bounds.width/6, y: self.view.bounds.height/3.48) self.view.addSubview(myImageView5) myImageView6 = UIImageView(frame: CGRectMake(0,0,30,30)) ; myImageView6.layer.position = CGPoint(x: self.view.bounds.width/3.8, y: self.view.bounds.height/3.48) self.view.addSubview(myImageView6) myImageView7 = UIImageView(frame: CGRectMake(0,0,30,30)) myImageView7.backgroundColor = UIColor.redColor(); myImageView7.layer.position = CGPoint(x: self.view.bounds.width/2.8, y: self.view.bounds.height/3.48) self.view.addSubview(myImageView7) myImageView8 = UIImageView(frame: CGRectMake(0,0,30,30)) myImageView8.layer.position = CGPoint(x: self.view.bounds.width/2.22, y: self.view.bounds.height/3.48) self.view.addSubview(myImageView8) myImageView9 = UIImageView(frame: CGRectMake(0,0,30,30)) myImageView9.layer.position = CGPoint(x: self.view.bounds.width/6, y: self.view.bounds.height/2.86) self.view.addSubview(myImageView9) myImageView10 = UIImageView(frame: CGRectMake(0,0,30,30)) myImageView10.layer.position = CGPoint(x: self.view.bounds.width/3.8, y: self.view.bounds.height/2.86) self.view.addSubview(myImageView10) myImageView11 = UIImageView(frame: CGRectMake(0,0,30,30)) myImageView11.layer.position = CGPoint(x: self.view.bounds.width/2.8, y: self.view.bounds.height/2.86) self.view.addSubview(myImageView11) myImageView12 = UIImageView(frame: CGRectMake(0,0,30,30)) myImageView12.layer.position = CGPoint(x: self.view.bounds.width/2.22, y: self.view.bounds.height/2.86) self.view.addSubview(myImageView12) myImageView13 = UIImageView(frame: CGRectMake(0,0,30,30)) myImageView13.layer.position = CGPoint(x: self.view.bounds.width/6, y: self.view.bounds.height/2.43) self.view.addSubview(myImageView13) myImageView14 = UIImageView(frame: CGRectMake(0,0,30,30)) myImageView14.backgroundColor = UIColor.greenColor(); myImageView14.layer.position = CGPoint(x: self.view.bounds.width/3.8, y: self.view.bounds.height/2.43) self.view.addSubview(myImageView14) myImageView15 = UIImageView(frame: CGRectMake(0,0,30,30)) myImageView15.layer.position = CGPoint(x: self.view.bounds.width/2.8, y: self.view.bounds.height/2.43) self.view.addSubview(myImageView15) myImageView16 = UIImageView(frame: CGRectMake(0,0,30,30)) myImageView16.layer.position = CGPoint(x: self.view.bounds.width/2.22, y: self.view.bounds.height/2.43) self.view.addSubview(myImageView16) myImageViewA = UIImageView(frame: CGRectMake(0,0,30,30)) myImageViewA.backgroundColor = UIColor.redColor(); myImageViewA.layer.position = CGPoint(x: self.view.bounds.width/3, y: self.view.bounds.height/1.2) self.view.addSubview(myImageViewA) myImageViewB = UIImageView(frame: CGRectMake(0,0,30,30)) myImageViewB.backgroundColor = UIColor.greenColor(); myImageViewB.layer.position = CGPoint(x: self.view.bounds.width/2, y: self.view.bounds.height/1.2) self.view.addSubview(myImageViewB) myImageViewC = UIImageView(frame: CGRectMake(0,0,30,30)) myImageViewC.backgroundColor = UIColor.blueColor(); myImageViewC.layer.position = CGPoint(x: self.view.bounds.width/1.5, y: self.view.bounds.height/1.2) self.view.addSubview(myImageViewC) myImageView.userInteractionEnabled = true myImageView2.userInteractionEnabled = true myImageView3.userInteractionEnabled = true myImageView4.userInteractionEnabled = true myImageView5.userInteractionEnabled = true myImageView6.userInteractionEnabled = true myImageView7.userInteractionEnabled = true myImageView8.userInteractionEnabled = true myImageView9.userInteractionEnabled = true myImageView10.userInteractionEnabled = true myImageView11.userInteractionEnabled = true myImageView12.userInteractionEnabled = true myImageView13.userInteractionEnabled = true myImageView14.userInteractionEnabled = true myImageView15.userInteractionEnabled = true myImageView16.userInteractionEnabled = true }

}
イメージ説明

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

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

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

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

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

guest

回答1

0

このようなケースの場合、UIImageViewプロパティを必要なだけ作成する方法よりも、UICollectionViewを使用した方が効率的です。UICollectionViewはUITableViewと同じような方法(Datasource,Delegateを用いて)で使用できます。

1 UICollectionViewControllerを継承した任意の名前のControllerを新規作成します。

2 以下の二つのDatasoureメソッドを実装します。セルの作成時に実行されるメソッドです。

override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int{
return 16(必要なマス目の数を指定します。添付なさった画像から16と推察しました)
}

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell{ let cell : UICollectionViewCell = collectionView.dequeueReusableCellWithReuseIdentifier("CollectionViewCell", forIndexPath: indexPath) cell.contentView.backgroundColor = = UIColor.whiteColor() return cell }

3 最後に以下のdelegateメソッドを実装します。セルをタップした時に実行されるメソッドです。

override func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) {
let cell:UICollectionViewCell = collectionView.cellForItemAtIndexPath(indexPath)!

}

画面下部の赤・緑・青のいずれかの画像がユーザによって選択されたフラグを立て、

override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell

上記メソッドの中でcellの背景色を都度変えてあげれば、ご希望の動作が実現できると思います。

投稿2015/11/21 15:11

Unixer

総合スコア143

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだベストアンサーが選ばれていません

会員登録して回答してみよう

アカウントをお持ちの方は

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問