I am new to iphone and xcode. This may be a very simple question but I could not find an example code in books. I want to do the following:
- Have 2 UIView on the screen
- Each view will step through the color of the rainbow (red/orange/yellow/green/blue/indigo/violet) using gesture recognizer, e.g., if the current color is green, if the user swipes up the UIView changes to yellow, if the user swipes down the UIView changes to blue.
- Hence, each view will need to keep the current color and respond to the swipes accordingly.
I understand how to implement the detection of swipes using gesture recognizer but I don’t know how to have each view keep a separate variable for the current color. I want a generic code because there will be more than 2 UIViews in my application once I figure out how it is done.
Thanks in advance.
Since you are going to maintain an array of colors, say
colorsArray, and assign views their color from that selection. You can do this in the swipe handler.This way you don’t need to subclass.
Subclassing
You can subclass
UIViewand add your own instance variables to it. Say,In your gesture handling method,