1 #import <UIKit/UIKit.h> 2 3 @interface FKCustomView : UIView 4 5 @end 6 7 CustomView.m 8 #import "FKCustomView.h" 9 10 @implementation FKCustomView 11 12 //定义两个变量几率当前触碰点的坐标 13 int curX; 14 int curY; 15 -(void) touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event 16 { 17 //获取触碰事件的UITouch事件 18 UITouch *touch = [touches anyObject]; 19 //得到触碰事件在当前组件上的触碰点 20 CGPoint lastTouch = [touch locationInView:self]; 21 //获取触碰点的坐标 22 curX = lastTouch.x; 23 curY = lastTouch.y; 24 [self setNeedsDisplay]; 25 } 26 -(void)drawRect:(CGRect)rect 27 { 28 //获取绘图上下文 29 CGContextRef ctx = UIGraphicsGetCurrentContext(); 30 //设置填充颜色 31 CGContextSetFillColorWithColor(ctx, [[UIColor redColor]CGColor]); 32 //以触碰点为圆心,绘制一个圆形 33 CGContextFillEllipseInRect(ctx, CGRectMake(curX-10, curY-10, 20, 20)); 34 } 35 @end
在控制器类中将该view添加进去
@interface xxx
@property (nonatomic,strong)FKCustomView *custView;
---------------
@implements xxx
_custView = [[FKCustomView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
_custView.backgroundColor = [UIColor blackColor];
[self.view addSubview:_custView];