原文网址:http://blog.csdn.net/baidu_nod/article/details/32934565
先看下页面的效果图:

首先定义这个ball它有两个属性和两个方法:
 
@property(nonatomic) CGPoint location;
@property(nonatomic) CGFloat length;
-(CGPoint) getCenterPoint;
-(BOOL) isInTheBall:(CGPoint) point;
 
方法体是:
 
- -(CGPoint) getCenterPoint {  
-       
-     return CGPointMake((self.location.x+self.length/2), self.location.y+self.length/2);  
- };  
-   
-   
- -(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;  
- @property(nonatomic) CGPoint prePoint;  
- - (id)initWithBall:(CGRect)frame aBall:(Ball*) ball; 
 
初始化函数为:
 
 
- - (id)initWithBall:(CGRect)frame aBall:(Ball*) ball  
- {  
-     self = [super initWithFrame:frame];  
-     if (self) {  
-         
-         self.ball = ball;  
-     }  
-     return self;  
- }  
-   
- -(void)awakeFromNib{  
-     self.backgroundColor = nil;  
-     self.opaque = NO;  
- }  
-   
- - (void)drawRect:(CGRect)rect  
- {  
-     
-     [super drawRect:rect];  
-       
-     CGContextRef contextRef = UIGraphicsGetCurrentContext();  
-     [[UIColor whiteColor] set];  
-       
-     
-     CGContextFillRect(contextRef, rect);  
-       
-     [[UIColor redColor] set];  
-       
-     
-    
-     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);  
-         
-         [self setNeedsDisplayInRect:preRect];  
-           
-         UITouch* touch = [touches anyObject];  
-         CGPoint point = [touch locationInView:self];  
-           
-         
-         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);  
-         
-         [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
 
【转】IOS的处理touch事件处理(依照手指的移动移动一个圆,开发环境用的ios7,storyboard)-- 不错
原文:http://www.cnblogs.com/wi100sh/p/5554397.html