OC利用delegate实现霓虹灯效果

#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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值