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

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;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容