2011-12-23 2 views
2

Прежде всего, этот код работает отлично, он делает именно то, что мне нужно, но, просмотрев его, я что-то упускаю.Перемещение UIView на экране, почему это работает?

У нас есть карточка. При касании карта перемещается. Когда в каком-то месте он отправляется в это место.

-(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { 
    ... 
    // Take note of the starting position and current position, both the same 
    startTouchPoint = pt; 
    currentTouchPoint = pt; 
    ...  
} 

-(void) touchesMoved:(NSSet *) touches withEvent:(UIEvent *)event { 
    ... 
    // As touch moves around the screen, current touch point is 
    // updated with it's new location 
    currentTouchPoint = [first locationInView:self]; 
    ... 
} 

-(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event { 
    ... 
    // If something happens, put card in a given location 
    cardCurrentlyBeingTouched.xPosition = 300; 
    cardCurrentlyBeingTouched.yPosition = 150; 
    ... 
} 

-(void) drawRect:(CGRect)rect { 
    ... 
    // Card moving around the screen 
    cardCurrentlyBeingTouched.xPosition += (self.currentTouchPoint.x - startTouchPoint.x); 
    cardCurrentlyBeingTouched.yPosition += (self.currentTouchPoint.y - startTouchPoint.y); 

    CGPoint moveToLoc = CGPointMake(cardCurrentlyBeingTouched.xPosition, cardCurrentlyBeingTouched.yPosition); 

    [self addSubview:[cardCurrentlyBeingTouched uiImageViewAtPoint:moveToLoc rotated:0 withFaceImage:YES]]; 

    startTouchPoint.x = currentTouchPoint.x; 
    startTouchPoint.y = currentTouchPoint.y; 
    ... 
} 

Так, в drawRect (во все времена) self.currentTouchPoint.x такое же, как startTouchPoint.x (как Y), как в X мировой карты и Y позиции меняются, если дельта всегда 0? (позиции X и Y равны CGPoints, когда я распечатал их для сравнения, я напечатал поплавки)

Может ли это быть точной проблемой?

Это мой код, я написал его, он работает. Но почему? :)

ответ

1

Как видно из моментального снимка ниже, нет никакого чуда в конце концов. Урок здесь состоит в том, что drawRect называется ужасно часто (что, конечно, хорошо)

> 2011-12-22 21:38:56.912 myApp[8923:f803] -- 266.000000 and 266.000000 
> 2011-12-22 21:38:56.913 myApp[8923:f803] -- 266.000000 and 266.000000 
> 2011-12-22 21:38:56.915 myApp[8923:f803] -- 282.000000 and 266.000000 <--- 
> 2011-12-22 21:38:56.922 myApp[8923:f803] -- 282.000000 and 282.000000 
> 2011-12-22 21:38:56.923 myApp[8923:f803] -- 282.000000 and 282.000000 
> 2011-12-22 21:38:56.925 myApp[8923:f803] -- 282.000000 and 282.000000 
> 2011-12-22 21:38:56.932 myApp[8923:f803] -- 282.000000 and 282.000000 
> 2011-12-22 21:38:56.933 myApp[8923:f803] -- 282.000000 and 282.000000 
> 2011-12-22 21:38:56.934 myApp[8923:f803] -- 293.000000 and 282.000000 <--- 
> 2011-12-22 21:38:56.941 myApp[8923:f803] -- 293.000000 and 293.000000 
> 2011-12-22 21:38:56.942 myApp[8923:f803] -- 293.000000 and 293.000000 
> 2011-12-22 21:38:56.943 myApp[8923:f803] -- 293.000000 and 293.000000 
> 2011-12-22 21:38:56.950 myApp[8923:f803] -- 293.000000 and 293.000000 
> 2011-12-22 21:38:56.951 myApp[8923:f803] -- 293.000000 and 293.000000 
> 2011-12-22 21:38:56.952 myApp[8923:f803] -- 308.000000 and 293.000000 <--- 
> 2011-12-22 21:38:56.958 myApp[8923:f803] -- 308.000000 and 308.000000 
> 2011-12-22 21:38:56.959 myApp[8923:f803] -- 308.000000 and 308.000000 
> 2011-12-22 21:38:56.960 myApp[8923:f803] -- 320.000000 and 308.000000 <--- 
> 2011-12-22 21:38:56.975 myApp[8923:f803] -- 320.000000 and 320.000000 
> 2011-12-22 21:38:56.976 myApp[8923:f803] -- 320.000000 and 320.000000 
> 2011-12-22 21:38:56.977 myApp[8923:f803] -- 330.000000 and 320.000000 
> 2011-12-22 21:38:56.987 myApp[8923:f803] -- 330.000000 and 330.000000 
> 2011-12-22 21:38:56.988 myApp[8923:f803] -- 330.000000 and 330.000000 
> 2011-12-22 21:38:56.990 myApp[8923:f803] -- 339.000000 and 330.000000 <--- 
> 2011-12-22 21:38:56.999 myApp[8923:f803] -- 339.000000 and 339.000000 
> 2011-12-22 21:38:57.001 myApp[8923:f803] -- 339.000000 and 339.000000 
> 2011-12-22 21:38:57.002 myApp[8923:f803] -- 339.000000 and 339.000000 
> 2011-12-22 21:38:57.012 myApp[8923:f803] -- 339.000000 and 339.000000 
> 2011-12-22 21:38:57.013 myApp[8923:f803] -- 339.000000 and 339.000000 
> 2011-12-22 21:38:57.015 myApp[8923:f803] -- 349.000000 and 339.000000 <--- 
Смежные вопросы