刮刮乐

作者: RainyGY | 来源:发表于2016-06-28 15:17 被阅读12次

    设置刮开后,显示的文字Label

    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(7,50,400,400)]; label.text = @"离思五首\n元稹\n曾经沧海难为水,\n除却巫山不是云!\n取次花丛懒回顾,\n半缘修道半缘君!\n";

     label.numberOfLines =0;

     label.backgroundColor = [UIColor colorWithRed:(arc4random()%173)/346.0+0.5green:(arc4random()%173)/346.0+0.5blue:(arc4random()%173)/346.0+0.5alpha:1];

     label.font = [UIFont systemFontOfSize:30]; 

    label.textAlignment = NSTextAlignmentCenter; [self.view addSubview:label];

    设置遮挡在外面的Image

    self.imageView= [[UIImageViewalloc]initWithFrame:CGRectMake(7,50,400,400)];self.imageView.image= [UIImageimageNamed:@"可达鸭"]; 

    [self.viewaddSubview:self.imageView];

    在touchesMoved方法里面实现操作

    `- (void)touchesMoved:(NSSet

    *)touches withEvent:(UIEvent*)event{

    // 触摸任意位置

    UITouch*touch = touches.anyObject;

    // 触摸位置在图片上的坐标CGPointcententPoint = [touch locationInView:self.imageView];

    // 设置清除点的大小CGRectrect =CGRectMake(cententPoint.x, cententPoint.y,20,20);

    // 默认是去创建一个透明的视图    UIGraphicsBeginImageContextWithOptions(self.imageView.bounds.size, NO, 0);

    // 获取上下文(画板)CGContextRefref =UIGraphicsGetCurrentContext();

    // 把imageView的layer映射到上下文中[self.imageView.layerrenderInContext:ref];

    // 清除划过的区域CGContextClearRect(ref, rect);

    // 获取图片UIImage*image =UIGraphicsGetImageFromCurrentImageContext();// 结束图片的画板, (意味着图片在上下文中消失)UIGraphicsEndImageContext();self.imageView.image= image;

    著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”。

    相关文章

      网友评论

          本文标题:刮刮乐

          本文链接:https://www.haomeiwen.com/subject/ggetjttx.html