您的位置:首页 > 产品设计 > UI/UE

UI_炫彩方框,旋转跑马灯,炫彩画板

2016-01-23 14:29 495 查看
#import "AppDelegate.h"
#import "ViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

   

    

    
    ViewController *viewController = [[ViewController alloc]init];
    self.window.rootViewController = viewController;

    

    

    

    

    
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
}

#import "ViewController.h"
#import "TouchView.h"
#import "PaintView.h"
#import "NeonlightView.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
/*

 //炫彩方框框
    TouchView *touchView = [[TouchView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];

    //关闭用户交互,阻断响应者链
    touchView.userInteractionEnabled = NO;
    touchView.center = self.view.center;
    touchView.backgroundColor = [UIColor yellowColor];
    [self.view addSubview:touchView];
*/
  /*
    //彩色笔画画
    PaintView *paintView = [[PaintView alloc]initWithFrame:self.view.frame];
    [paintView setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:paintView];
*/

    

    //旋转圆圈圈
    NeonlightView *neonlightView = [[NeonlightView alloc]initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), CGRectGetWidth(self.view.frame))];
    neonlightView.backgroundColor = [UIColor whiteColor];
    neonlightView.center = self.view.center;
    [self.view addSubview:neonlightView];

    

    

    

    
}

#import "TouchView.h"

@implementation TouchView

//每一个UIView或者UIView的子类都可以实现触摸事件,前提是得重写系统提供的方法

//触摸开始,触摸对象(手指)触摸到屏幕
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    self.backgroundColor = [UIColor redColor];
    NSLog(@"%s",__FUNCTION__);
}

//触摸对象在屏幕上移动
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

    self.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0  blue:arc4random()%256/255.0  alpha:1];
    NSLog(@"%s",__FUNCTION__);
        //得到触摸对象
    UITouch *touch = touches.anyObject;

        //得到触摸对象当前所在的点
    CGPoint locationPoint = [touch locationInView:self.superview];
        //得到移动之前的点
    CGPoint prePoint = [touch previousLocationInView:self.superview];

    
        //要计算delX和delY所移动的距离,x和y所移动的距离
    float delx = locationPoint.x - prePoint.x;
    float dely = locationPoint.y - prePoint.y;

    

        //通过改变中心点;来改变当前视图的位置,xy移动的距离就是中心点移动的距离
//    self.center = CGPointMake(self.center.x +delx, self.center.y +dely);

    

    //获得整个屏幕的宽度和长度
    float screenWidth = [UIScreen mainScreen].bounds.size.width;
    float screenHeight = [UIScreen mainScreen].bounds.size.height;
    if (self.center.x >=100 && self.center.x <=screenWidth - 100 &&self.center.y >=100 &&self.center.y<=screenHeight-100) {

        
        //不让左边出屏幕
        float screenX = self.center.x +delx;
        if (screenX <100) {
            screenX = 100;
        }

       
        //刚好贴着右边的边界时,centerx = scree.widt-100;如果右边除屏幕的话,spcenter.xscreen.width
-100;如果右边出屏幕了话 center.x ==senter.width  - 100+向右移动的距离,这个时候center.x就会大于screen.width - 100
        if (screenX >screenWidth - 100) {
            screenX = screenWidth -100;
        }
        self.center = CGPointMake(screenX,self.center.y +dely);

   

        
        float screenY = self.center.y + dely;
        //不让上边出屏幕
        if (screenY < 100) {
            screenY = 100;
        }
        //不让下边出屏幕
        if (screenY > (screenHeight - 100)) {
            screenY = screenHeight - 100;
        }

        //移动过程中,改变当前视图的大小
        CGRect rect = self.frame;
        rect.size.width = rect.size.width + delx;
        rect.size.height = rect.size.height + dely;
        self.frame = rect;
        self.center = CGPointMake(screenX, screenY);
    }
        NSLog(@"x----%f", self.center.x);
        NSLog(@"y----%f", self.center.y);
        NSLog(@"%s", __func__);

    

    
}

//触摸对象离开屏幕
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
    self.backgroundColor = [UIColor blackColor];
    NSLog(@"%s",__FUNCTION__);
}

//当触摸被打断时,例如:正在触摸的时候,来电话,来短信等
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{

    NSLog(@"%s",__FUNCTION__);
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end

#import "PaintView.h"

@interface PaintView ()

@property(nonatomic,retain)NSMutableArray *allLinesMutableArray;  //存储所有线条
@property(nonatomic,retain)NSMutableArray *allColorMutableArray;  //存储所有颜色
@end
@implementation PaintView

//将属性数组的懒加载(调用getter方法)
-(NSMutableArray *)allLinesMutableArray{

    if (!_allLinesMutableArray) {
        _allLinesMutableArray = [NSMuta
f46a
bleArray array];
    }
    return _allLinesMutableArray;
}

//颜色数组的懒加载
-(NSMutableArray *)allColorMutableArray{
    if (!_allColorMutableArray) {
        _allColorMutableArray = [[NSMutableArray alloc]init];
    }
    return _allColorMutableArray;
}

//创建一个按钮的懒加载
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if(self){
        UIButton *myButton = [UIButton buttonWithType:UIButtonTypeSystem];
        [myButton setTitle:@"橡皮擦" forState:UIControlStateNormal];
        [myButton setFrame:CGRectMake(20, 20, 50 , 30)];
        [myButton addTarget:self action:@selector(deletelLineAction:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:myButton];

        
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        [button setTitle:@"重画" forState:UIControlStateNormal];
        [button addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
        [button setFrame:CGRectMake(220, 20, 50, 30)];
        [self addSubview:button];

        
    }
    return self;
}

//按钮的回调方法,用来删除线条
-(void)deletelLineAction:(UIButton *)sender{
    if (self.allLinesMutableArray.count) {
        [self.allLinesMutableArray removeLastObject];
        [self setNeedsDisplay];
    }
}
-(void)backAction:(UIButton *)sender{
    if (self.allLinesMutableArray.count) {
        [self.allLinesMutableArray removeAllObjects];
        [self setNeedsDisplay];
    }
}

//触碰对象
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{

    

//得到绘图的起始点
    //得到触摸对象
    UITouch *touch = touches.anyObject;
    //得到起始点
    CGPoint startPoint = [touch locationInView:self.superview];

//将起始点存储到一条线中

    //初始化一条贝塞尔曲线
    UIBezierPath *bezierLine = [UIBezierPath bezierPath];

    //将起始点存储到贝塞尔曲线中
    [bezierLine moveToPoint:startPoint];

//将贝塞尔曲线存储到数组中
    [self.allLinesMutableArray addObject:bezierLine];

    //每新增一条线,就给该线条添加对应的颜色
    [self.allColorMutableArray addObject:[UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0]];
}

//触摸对象移动
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{

    

//触摸对象移动的时候,才会产生新的点,将这些所有点都存储到贝塞尔曲线中,才能真正的组成一条线
    //得到触摸对象
    UITouch *touch = touches.anyObject;
    //得到当前的点
    CGPoint locationPoint = [touch locationInView:self.superview];

    //取出刚才在开始触摸的方法中初始化好的贝塞尔曲线
    UIBezierPath *bezierLine = self.allLinesMutableArray.lastObject;

    //将得到的点放入贝塞尔曲线中
    [bezierLine addLineToPoint:locationPoint];

    //重新绘制当前界面
    [self setNeedsDisplay];  //调用此方法,系统会触发drawRect方法,重新绘制当前界面

    

    

    
}

//触摸对象离开屏幕
-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{

}

////打断触摸
-(void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{

}

//绘制当前界面
-(void)drawRect:(CGRect)rect{
    //设置画笔
//    [[UIColor redColor]setStroke];

//开始划线

    //遍历所有的线条,开始绘制
    for (UIBezierPath * line in self.allLinesMutableArray) {

    

        //当前对象在数组中的下标
        NSInteger index = [self.allLinesMutableArray indexOfObject:line];
        UIColor *lineColor = [self.allColorMutableArray objectAtIndex:index];

      
        //设置每条线的画笔
        [lineColor setStroke];
        //设置画笔粗细
        [line setLineWidth:20];
        //开始绘制
        [line stroke];
    }

    
}

@end

#import "NeonlightView.h"

@implementation NeonlightView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        for (int i = 0; i<10; i++) {
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(frame)-30*i, CGRectGetHeight(frame)-30*i)];
            view.backgroundColor = [UIColor colorWithRed:arc4random()%256/255.0 green:arc4random()%256/255.0 blue:arc4random()%256/255.0 alpha:1.0];
            view.tag = 1000+i;
            view.center = self.center;
            //将当前视图设置为圆形 
半径
            view.layer.cornerRadius = CGRectGetHeight(view.frame)/2;
            [self addSubview:view];
            }

        
        //定时器
        //interval:设置多长时间调用一次回调方法,单位为秒
        //target: 
执行回调方法的目标
        //selector:回调方法
        //userinfo:回调方法的参数
        //repeats:是否重复执行---YES or NO
        [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(changeColor) userInfo:nil repeats:YES];
    }
    return self;
}
-(void)changeColor{

    //每执行一次,由小到大的视图的颜色交换一遍
    for (int i = 0; i<9; i++) {
        //得到某一个视图的颜色
        UIColor *myColor = [[self viewWithTag:1000+i] backgroundColor];
        //得到下一个视图的颜色
        UIColor *nextColor = [[self viewWithTag:1000+i+1]backgroundColor];
        //交换视图颜色
        [[self viewWithTag:1000+i] setBackgroundColor:nextColor];
        [[self viewWithTag:1000+i+1 ] setBackgroundColor:myColor];
    }
}

@end
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: