码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2015-11-28 18:12:26      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:

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

技术分享

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

标签:

原文地址:http://www.cnblogs.com/yanggenwei/p/5002989.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!