"Go" processing touch event handling for iOS (move a circle according to the movement of your finger, ios7,storyboard for development environment)--good

Source: Internet
Author: User

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  
  1. Find the center point of Ball
  2. -(Cgpoint) Getcenterpoint {
  3. return Cgpointmake (self.x+-length/2), self . Location. y+  self. length/2);
  4. };
  5. The point of point is not within Ball's range
  6. -(BOOL) Isintheball: (cgpoint) point{
  7. Cgpoint Center = self. getcenterpoint;
  8. float t = (point.x-centerx) * (Pointx-center. x);
  9. Float y = (point.y-centery) * (pointy-centery);
  10. float k = sqrtf (t+y);
  11. if (K < self. length/2) {
  12. return YES;
  13. }Else {
  14. return NO;
  15. }
  16. };


Define Ballview Inheritance UIView

[OBJC]View PlainCopy 
    1. @property (nonatomic) ball* ball;
    2. @property (nonatomic) BOOL Istouch; //Indicates that the finger moves within the ball's range
    3. @property (nonatomic) Cgpoint prepoint; //finger at the point before entering the move event
    4. -(ID) Initwithball: (cgrect) frame Aball: (ball*) ball; //Initialize method


The initialization function is:

[OBJC]View PlainCopy 
  1. -(ID) Initwithball: (cgrect) frame Aball: (ball*) Ball
  2. {
  3. Self = [super Initwithframe:frame];
  4. if (self) {
  5. //initialization code
  6. self. Ball = ball;
  7. }
  8. return self ;
  9. }
  10. -(void) awakefromnib{
  11. self. backgroundcolor = Nil;
  12. self. opaque = NO;
  13. }
  14. Only override Drawrect:if perform custom drawing.
  15. An empty implementation adversely affects performance during animation.
  16. -(void) DrawRect: (cgrect) Rect
  17. {
  18. //Drawing code
  19. [Super Drawrect:rect];
  20. Cgcontextref contextref = Uigraphicsgetcurrentcontext ();
  21. [[Uicolor Whitecolor] set];
  22. //rect is the entire view
  23. Cgcontextfillrect (Contextref, rect);
  24. [[Uicolor Redcolor] set];
  25. //cgcontextaddellipseinrect does not fill the interior of the circle
  26. //Cgcontextaddellipseinrect (Contextref, CGRectMake (200.0f, 200.0f, 50.0f, 50.0f));
  27. Cgcontextfillellipseinrect (Contextref, CGRectMake (self), locationx. x, self.  Location. Y,self. Balllength, self. Balllength);
  28. Cgcontextstrokepath (CONTEXTREF);
  29. }


We initialize in Viewcontroller as long as:

[OBJC]View PlainCopy 
  1. -(void) loadview{
  2. [Super Loadview];
  3. ball* ball = [[Ball Alloc] init];
  4. Ball. Location = Cgpointmake (200. 0f, 100. 0f);
  5. Ball. length = 80. 0f;
  6. ballview* view = [[Ballview alloc] initwithball:[uiscreen mainscreen]. Bounds Aball:ball];
  7. [self. View Addsubview:view];
  8. }


The event is then handled in Ballview below.

[OBJC]View PlainCopy  
  1. -(void) Touchesbegan: (nsset *) touches withevent: (uievent *) event{
  2. NSLog (@ "Touchesbegan");
  3. //The following two sentences know the point of the finger on the screen information
  4. uitouch* touch = [touches anyobject];
  5. Cgpoint point = [Touch Locationinview: self];
  6. if ([self. Ball Isintheball:point]) {
  7. self. Istouch = YES;
  8. self. prepoint = point;
  9. }else{
  10. self. Istouch = NO;
  11. }
  12. NSLog (@ "x=%f,y=%f", point. x,pointy);
  13. }
  14. -(void) touchesmoved: (nsset *) touches withevent: (uievent *) event{
  15. NSLog (@ "touchesmoved");
  16. if (self. Istouch) {
  17. CGRect Prerect = CGRectMake (self), locationx. x, self .   . Balllength, self . Ball. Length);
  18. //Use previous location to draw once
  19. [self setneedsdisplayinrect:prerect];
  20. uitouch* touch = [touches anyobject];
  21. Cgpoint point = [Touch Locationinview: self];
  22. //cx and Cy are the offsets of the fingers, so they can calculate the new location
  23. float cx = point. x- self. prepointx;
  24. float cy = point. Y- self. Prepointy;
  25.         self .ball.location = cgpointmake ( self.ball.location.x + cx, self.ball.location.y+cy)   
  26. CGRect Newrect = CGRectMake (self), locationx. x, self .   . Balllength, self . Ball. Length);
  27. //Use a new location to draw it again
  28. [self setneedsdisplayinrect:newrect];
  29. self. prepoint = point;
  30. }
  31. }
  32. -(void) touchesended: (nsset *) touches withevent: (uievent *) event{
  33. NSLog (@ "touchesended");
  34. self. Istouch = NO;
  35. }



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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.