IOS 自定义控件 跟随鼠标的圆点

 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];

转载于:https://www.cnblogs.com/yanggenwei/p/5002989.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值