2011-10-16 5 views
1

사용자가 화면에서 드래그해야하는 객체가 있습니다. 이것은 일반적인 UIImageView입니다. 현재 이미지를 드래그 할 수 있지만 놓으면 손가락이 이미지를 들었을 때 멈 춥니 다. 내가 원하는 것은 기세를 창출하는 것이므로 이미지에 충동을 줄 수 있고 화면 가장자리가 튀어 나올 때까지 스크롤 할 수 있습니다.iPhone - 관성 끌기

필자는 물리 라이브러리를 추가하는 것과 같은 복잡한 작업을 원하지 않습니다. 가능한 한 최소로 유지하고 싶습니다.

어떻게하면됩니까? 너희들이 나를 튜토리얼이나 올바른 방향으로 가리킬 수 있니?

감사합니다. 당신의 - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 방법보다

CFTimeInterval startTime; 
CGPoint startPoint; 
CGPoint oldPoint; 
BOOL imageViewTouched; 

이 코드를 가지고 :

클래스의 첫 번째

은 다음과 같이 4 개 인스턴스 변수를 만들 : 어떤 물리 라이브러리를 사용하지 않고이 일을

답변

3

그런데 한 가지 방법이있다

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    UITouch *touch = [[event allTouched] anyObject]; 
    // Test to see if the touched view is your image view. If not just return. 
    if (touch.view != <#yourImageView#>) { 
     return; 
    } 

    startPoint = [touch locationInView:self.view]; 
    oldPoint = startPoint; 
    startTime = CACurrentMediaTime(); 
    imageViewTouched = YES; 
} 

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event에 대해이 작업을 수행 :

12,지금 - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event이 시도 :

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event 
{ 
    // Fingers were removed from the screen but your image view was not touched in the beginning 
    if (!imageViewTouched) { 
     return; 
    } 

    imageViewTouched = NO; 

    UITouch *touch = [[event allTouches] anyObject]; 
    CGPoint endPoint = [touch locationInView:self.view]; 
    CFTimeInterval endTime = CACurrentMediaTime(); 

    CFTimeInterval timeDifference = endTime - startTime; 

    // You may play with this value until you get your desired effect 
    CGFloat maxSpeed = 80; 

    CGFloat deltaX = 0; 
    CGFloat deltaY = 0; 

    if (timeDifference < 0.35) { 
     deltaX = (endPoint.x - startPoint.x)/(timeDifference * 10); 
     deltaY = (endPoint.y - startPoint.y)/(timeDifference * 10); 
    } 

    if  (deltaX > maxSpeed)   { deltaX = maxSpeed; } 
    else if (deltaX < -maxSpeed)  { deltaX = -maxSpeed; } 
    else if (deltaX > -5 && deltaX < 5) { deltaX = 0; } 

    if  (deltaY > maxSpeed)   { deltaY = maxSpeed; } 
    else if (deltaY < -maxSpeed)  { deltaY = -maxSpeed; } 
    else if (deltaY > -5 && deltaY < 5) { deltaY = 0; } 

    [UIView begginAnimations:nil context:nil]; 
    [UIView setAnimationDuration:0.5f]; 
    [UIView setAnimationCurve:UIViewAnimationCurveEaseOut]; 

    <#yourImageView#>.frame = CGRectOffset(<#yourImageView#>.frame, deltaX, deltaY); 

    [UIView commitAnimations]; 
} 

이 말이 및/또는 당신을 위해 작동하는지 알려 주시기 바랍니다. 건배!

+0

와우 !!!!!!!!!!!!!! 환상적이다, 고마워! 당신은 남자 야 !!!! – SpaceDog

관련 문제