#import <UIKit/UIKit.h>
@protocol colorChangeDelegate <NSObject>
- (void)colorChange;
@end
@interface NewView : UIView
@property (nonatomic, retain)id<colorChangeDelegate>delegate;
@end
#import "NewView.h"
@implementation NewView
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
for (int i = 0; i < 4; i++) {
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width * (4 - i) / 5, self.frame.size.height * (4 - i) / 5)];
view.center = self.center;
view.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];
view.tag = 4 - i;
[self addSubview:view];
[view release];
}
}
return self;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.delegate colorChange];
}
@end
#import <UIKit/UIKit.h>
#import "NewView.h"
@interface MainViewController : UIViewController<colorChangeDelegate>
@property (nonatomic, retain)NSTimer *timer;
@property (nonatomic, assign)BOOL click;
@property (nonatomic, assign)NSInteger style;
@end
#import "MainViewController.h"
#import "NewView.h"
@interface MainViewController ()
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
_style = 5;
self.view.backgroundColor = [UIColor whiteColor];
NewView *view = [[NewView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width)];
view.backgroundColor = [UIColor blackColor];
view.tag = 5;
view.delegate = self;
[self.view addSubview:view];
[view release];
// Do any additional setup after loading the view.
}
-(void)colorChange {
if (!_click) {
_timer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(colorDidChange) userInfo:nil repeats:YES];
_click = !_click;
}
else {
_click = !_click;
[_timer invalidate];
}
}
- (void)colorDidChange {
self.view.backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:arc4random() % 256 / 255.0];
[self.view viewWithTag:_style].backgroundColor = [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];
_style--;
if (_style == 1) {
_style = 5;
}
}
@end