Original URL: http://blog.csdn.net/baidu_nod/article/details/32934565
First look at the following page:
First define this ball it has two properties and two methods:
@property(nonatomic) cgpoint location ;
@property(nonatomic) cgfloat length;
-(cgpoint) getcenterpoint;
-(BOOL) Isintheball: (cgpoint) point;
The method body is:
[OBJC]View PlainCopy
- Find the center point of Ball
- -(Cgpoint) Getcenterpoint {
- return Cgpointmake (self.x+-length/2), self . Location. y+ self. length/2);
- };
- The point of point is not within Ball's range
- -(BOOL) Isintheball: (cgpoint) point{
- Cgpoint Center = self. getcenterpoint;
- float t = (point.x-centerx) * (Pointx-center. x);
- Float y = (point.y-centery) * (pointy-centery);
- float k = sqrtf (t+y);
- if (K < self. length/2) {
- return YES;
- }Else {
- return NO;
- }
- };
Define Ballview Inheritance UIView
[OBJC]View PlainCopy
- @property (nonatomic) ball* ball;
- @property (nonatomic) BOOL Istouch; //Indicates that the finger moves within the ball's range
- @property (nonatomic) Cgpoint prepoint; //finger at the point before entering the move event
- -(ID) Initwithball: (cgrect) frame Aball: (ball*) ball; //Initialize method
The initialization function is:
[OBJC]View PlainCopy
- -(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 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 is the entire view
- Cgcontextfillrect (Contextref, rect);
- [[Uicolor Redcolor] set];
- //cgcontextaddellipseinrect does not fill the interior of the circle
- //Cgcontextaddellipseinrect (Contextref, CGRectMake (200.0f, 200.0f, 50.0f, 50.0f));
- Cgcontextfillellipseinrect (Contextref, CGRectMake (self), locationx. x, self. Location. Y,self. Balllength, self. Balllength);
- Cgcontextstrokepath (CONTEXTREF);
- }
We initialize in Viewcontroller as long as:
[OBJC]View PlainCopy
- -(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];
- }
The event is then handled in Ballview below.
[OBJC]View PlainCopy
- -(void) Touchesbegan: (nsset *) touches withevent: (uievent *) event{
- NSLog (@ "Touchesbegan");
- //The following two sentences know the point of the finger on the screen information
- 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,pointy);
- }
- -(void) touchesmoved: (nsset *) touches withevent: (uievent *) event{
- NSLog (@ "touchesmoved");
- if (self. Istouch) {
- CGRect Prerect = CGRectMake (self), locationx. x, self . . Balllength, self . Ball. Length);
- //Use previous location to draw once
- [self setneedsdisplayinrect:prerect];
- uitouch* touch = [touches anyobject];
- Cgpoint point = [Touch Locationinview: self];
- //cx and Cy are the offsets of the fingers, so they can calculate the new location
- float cx = point. x- self. prepointx;
- float cy = point. Y- self. Prepointy;
- self .ball.location = cgpointmake ( self.ball.location.x + cx, self.ball.location.y+cy)
- CGRect Newrect = CGRectMake (self), locationx. x, self . . Balllength, self . Ball. Length);
- //Use a new location to draw it again
- [self setneedsdisplayinrect:newrect];
- self. prepoint = point;
- }
- }
- -(void) touchesended: (nsset *) touches withevent: (uievent *) event{
- NSLog (@ "touchesended");
- self. Istouch = NO;
- }
Code can be downloaded in http://download.csdn.net/detail/baidu_nod/7533317
Ios-day17-01 (UIView (Follow the finger Movement))
Original URL: http://www.ithao123.cn/content-7926067.html
Source: http://download.csdn.net/detail/liu537192/8544289
"Go" processing touch event handling for iOS (move a circle according to the movement of your finger, ios7,storyboard for development environment)--good