Поскольку вы собираетесь поддерживать массив цветов, скажем colorsArray
, и назначьте представлениям их цвет из этого выбора. Вы можете сделать это в обработчике прокрутки.
- (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];
}
Таким образом, вам не нужно создавать подклассы.
Наследование
Вы можете создать подкласс UIView
и добавить к нему собственные переменные экземпляра. Скажем,
@interface RainbowView: UIView {
NSInteger currentColorIndex;
}
@property (nonatomic, assign) NSInteger currentColorIndex;
...
@end
@implementation RainbowView
@synthesize currentColorIndex;
...
@end
В вашем методе обработки жестов,
- (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;
}