iPhone UIViews - how to associate variables with UIView?
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 (r开发者_StackOverflow中文版ed/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.
You can subclass UIView
to include any variables that you like:
ColorSwiperView.h
@interface ColorSwiperView : UIView
{
ColorType currentColor;
}
@property (nonatomic, assign) ColorType currentColor;
@end
ColorSwiperView.m
@implementation ColorSwiperView
@synthesize currentColor;
- (id)initWithFrame:(CGRect)frameRect
{
if ((self = [super initWithFrame:frameRect]) == nil) { return nil; }
currentColor = red;
return self;
}
@end
To be used as follows:
#import "ColorSwiperView"
...
ColorSwiperView * cView = [[ColorSwiperView alloc] initWithFrame:...];
cView.currentColor = green;
Note: this assumes that you have defined an enum for the colors:
typedef enum
{
red = 0,
green = 1,
...
}
ColorType;
perhaps defining a subclass of UIView something like:
@interface RainbowView : UIView {
UIColor *currentColor;
}
@property (nonatomic, retain) UIColor *currentColor;
@end
and creating 2 (or more) views of that class in your view controller as outlets (if you're using interface builder):
@class RainbowView;
@interface RainBowViewController : UIViewController {
RainbowView *rainbowView1;
RainbowView *rainbowView2;
}
@property (nonatomic, retain) IBOutlet RainbowView *rainbowView1;
@property (nonatomic, retain) IBOutlet RainbowView *rainbowView2;
@end
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.
- (void)handleSwipe:(UISwipeGestureRecognizer*)swipeGesture {
UIView *view = swipeGesture.view;
NSInteger currentColorIndex = [colorsArray indexOfObject:view.backgroundColor];
NSInteger nextColorIndex = currentColorIndex + 1;
if ( nextColorIndex == [colorsArray count] ) {
nextColorIndex = 0;
}
view.backgroundColor = [colorsArray objectAtIndex:nextColorIndex];
}
This way you don't need to subclass.
Subclassing
You can subclass UIView
and add your own instance variables to it. Say,
@interface RainbowView: UIView {
NSInteger currentColorIndex;
}
@property (nonatomic, assign) NSInteger currentColorIndex;
...
@end
@implementation RainbowView
@synthesize currentColorIndex;
...
@end
In your gesture handling method,
- (void)handleSwipe:(UISwipeGestureRecognizer*)swipeGesture {
RainbowView *aView = (RainbowView*)swipeGesture.view;
// Get the next color index to aView.currentColorIndex;
aView.backgroundColor = [colorsArray objectAtIndex:nextColorIndex];
aView.currentColorIndex = nextColorIndex;
}
精彩评论