美文网首页程序员iOS Developer
IOS:OC--小案例(刮刮乐的实现)

IOS:OC--小案例(刮刮乐的实现)

作者: 任任任任师艳 | 来源:发表于2017-06-13 17:09 被阅读0次

    ViewController.m

    import "ViewController.h"

    @interface ViewController ()
    @property(nonatomic,strong)UIImageView * imageV;
    @end

    @implementation ViewController

    • (void)viewDidLoad {
      [super viewDidLoad];
      // Do any additional setup after loading the view, typically from a nib.
      UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 200, 50)];
      label.text = @"因为你是彭于晏呀";
      label.textAlignment = NSTextAlignmentCenter;
      label.font = [UIFont fontWithName:nil size:50];

      [self.view addSubview:label];

      self.imageV = [[UIImageView alloc] initWithFrame:CGRectMake(100, 50, 200, 250)];
      self.imageV.image = [UIImage imageNamed:@"a.jpg"];
      [self.view addSubview:_imageV];
      //

    }
    //触摸且移动
    -(void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    //获取图片的任意一点
    UITouch * touch = touches.anyObject;
    

    CGPoint contentPoint = [touch locationInView:self.imageV];
    CGRect rect = CGRectMake(contentPoint.x, contentPoint.y, 10, 10);
    //开启图片上下文
    UIGraphicsBeginImageContextWithOptions(self.imageV.bounds.size, NO, 0);
    // 获取花瓣上下文
    CGContextRef ref = UIGraphicsGetCurrentContext();
    //嫁给你imgeV的layer层映射到上下文中
    [self.imageV.layer renderInContext:ref];
    //清除划过的区域
    CGContextClearRect(ref, rect);
    //获取图片
    UIImage * image = UIGraphicsGetImageFromCurrentImageContext();
    //结束画板,图片消失
    UIGraphicsEndPDFContext();
    self.imageV.image = image;

    }

    相关文章

      网友评论

        本文标题:IOS:OC--小案例(刮刮乐的实现)

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