原文网址:http://blog.csdn.net/baidu_nod/article/details/32934565
先看下页面的效果图:
首先定义这个ball它有两个属性和两个方法:
@property(nonatomic) CGPoint location;
@property(nonatomic) CGFloat length;
-(CGPoint) getCenterPoint;
-(BOOL) isInTheBall:(CGPoint) point;
方法体是:
- //找出ball的中心点
- -(CGPoint) getCenterPoint {
- return CGPointMake((self.location.x+self.length/2), self.location.y+self.length/2);
- };
- //看点point是不是在ball的范围内
- -(BOOL) isInTheBall:(CGPoint) point{
- CGPoint center = self.getCenterPoint;
- float t = (point.x - center.x) * (point.x - center.x);
- float y = (point.y - center.y) * (point.y - center.y);
- float k = sqrtf(t+y);
- if (k < self.length/2) {
- return YES;
- }else {
- return NO;
- }
- };
定义BallView继承UIView
- @property(nonatomic) Ball* ball;
- @property(nonatomic) BOOL isTouch; //表示手指在ball的范围内移动
- @property(nonatomic) CGPoint prePoint; //手指在进入move事件之前的那个点
- - (id)initWithBall:(CGRect)frame aBall:(Ball*) ball; //初始化方法
初始化函数为:
- - (id)initWithBall:(CGRect)frame aBall:(Ball*) ball
- {
- self = [super initWithFrame:frame];
- if (self) {
- // Initialization code
- self.ball = ball;
- }
- return self;
- }
- -(void)awakeFromNib{
- self.backgroundColor = nil;
- self.opaque = NO;
- }
- // Only override drawRect: if you perform custom drawing.
- // An empty implementation adversely affects performance during animation.
- - (void)drawRect:(CGRect)rect
- {
- // Drawing code
- [super drawRect:rect];
- CGContextRef contextRef = UIGraphicsGetCurrentContext();
- [[UIColor whiteColor] set];
- //rect是整个view
- CGContextFillRect(contextRef, rect);
- [[UIColor redColor] set];
- //CGContextAddEllipseInRect不会填充圆圈的内部
- // CGContextAddEllipseInRect(contextRef, CGRectMake(200.0f, 200.0f, 50.0f, 50.0f));
- CGContextFillEllipseInRect(contextRef, CGRectMake(self.ball.location.x,self.ball.location.y,self.ball.length,self.ball.length));
- CGContextStrokePath(contextRef);
- }
我们在viewController里初始化只要:
- -(void) loadView{
- [super loadView];
- Ball* ball = [[Ball alloc] init];
- ball.location = CGPointMake(200.0f, 100.0f);
- ball.length = 80.0f;
- BallView* view = [[BallView alloc] initWithBall:[UIScreen mainScreen].bounds aBall:ball];
- [self.view addSubview:view];
- }
然后在下面在BallView中进行事件处理
- -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
- NSLog(@"touchesBegan");
- //下面两句知道手指在屏幕上的点的信息
- UITouch* touch = [touches anyObject];
- CGPoint point = [touch locationInView:self];
- if ([self.ball isInTheBall:point]) {
- self.isTouch = YES;
- self.prePoint = point;
- }else{
- self.isTouch = NO;
- }
- NSLog(@"x=%f,y=%f",point.x,point.y);
- }
- -(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{
- NSLog(@"touchesMoved");
- if (self.isTouch) {
- CGRect preRect = CGRectMake(self.ball.location.x, self.ball.location.y, self.ball.length, self.ball.length);
- //先用之前的location绘制一遍
- [self setNeedsDisplayInRect:preRect];
- UITouch* touch = [touches anyObject];
- CGPoint point = [touch locationInView:self];
- //cx和cy是手指的偏移量,用他们可以计算出新的location
- float cx = point.x - self.prePoint.x;
- float cy = point.y - self.prePoint.y;
- self.ball.location = CGPointMake(self.ball.location.x + cx, self.ball.location.y+cy);
- CGRect newRect = CGRectMake(self.ball.location.x, self.ball.location.y, self.ball.length, self.ball.length);
- //用新的location绘制一遍
- [self setNeedsDisplayInRect:newRect];
- self.prePoint = point;
- }
- }
- -(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{
- NSLog(@"touchesEnded");
- self.isTouch = NO;
- }
代码可以在http://download.csdn.net/detail/baidu_nod/7533317下载
ios-day17-01(UIView的拖拽(跟随手指移动))
原文网址:http://www.ithao123.cn/content-7926067.html
源码下载地址:http://download.csdn.net/detail/liu537192/8544289