关闭

iOS中点击移动,使得手机屏幕的颜色随机变化

73人阅读 评论(0) 收藏 举报
分类:

1.效果是点击移动,整个屏幕的颜色随机变化

<pre name="code" class="objc">- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //鼠标移动时,随机获得颜色
    float red = arc4random() % 255;
    float green = arc4random() % 255;
    float blue = arc4random() % 255;
    UIColor *color = [UIColor colorWithRed:red/255 green:green/255 blue:blue/255 alpha:
                      1];
    ;
    self.view.backgroundColor = color;
}


2.效果是移动鼠标时,屏幕上创建出来一个view,只有view的颜色随机变化

- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    //从父视图上删除之前move所创建的view
    for (UIView *object in self.imageView.subviews){
        [object removeFromSuperview];
    }
    //创建一个点,来接收每次移动的点
    CGPoint point = [[touches anyObject] locationInView:self.imageView];
    //创建一个视图,每次移动,视图的中心都是move后所获的得point
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(10, 10, 80, 80)];
    //鼠标移动时,随机获得颜色
    float red = arc4random() % 255;
    float green = arc4random() % 255;
    float blue = arc4random() % 255;
    UIColor *color = [UIColor colorWithRed:red/255 green:green/255 blue:blue/255 alpha:
                      1];
    ;
    view.backgroundColor = color;
    [self.imageView addSubview:view];
    view.center = point;
  
}





0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:513次
    • 积分:41
    • 等级:
    • 排名:千里之外
    • 原创:4篇
    • 转载:0篇
    • 译文:0篇
    • 评论:0条
    文章分类
    文章存档