2010-02-22 6 views
0

iPhone/iPod Touch의 '슬라이드 위치'와 마찬가지로 드래그 가능한 UIImageView를 만듭니다. 잠금 해제 "슬라이더를 사용하십시오.view.center를 사용하지 않고도 1 축에서 UIImageView/UIView를 드래그하여 위치를 터치합니다. (어디에서든지 드래그하여 뷰를 드래그하여 x 축으로 이동)

다음을 사용하여보기를 x 축을 따라 원활하게 이동할 수 있습니다. float newCenter = touchPoint.x;

하지만 센터에서의 터치 오프셋을 고려하지 않았습니다. 단순히 뷰의 중심점을 터치 포인트로 이동하는 것입니다. UIImageView x 축을 따라 어떤 지점에서 끌어서보기 이동하려고합니다.

이하, 내에서 newCenter를 계산해 보았습니다. TouchesMoved하지만 부드럽게 슬라이드하지 못합니다.

코드 :

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event { 

    UITouch *touch = [touches anyObject]; 
    CGPoint touchPoint = [touch locationInView:self]; // self is a UIView 

    CGRect sliderRect = sliderView.frame; // sliderView is a UIImageView 

    if (CGRectContainsPoint(sliderRect, touchPoint)){ 

     float sliderHalfWidth = sliderView.frame.size.width/2; 
     float leftEdge = sliderView.center.x - sliderHalfWidth; 
     float distanceToTouchFromEdge = touchPoint.x - leftEdge; 

     float newCenter = (distanceToTouchFromEdge - sliderHalfWidth) + touchPoint.x; // <--- the math here is probably incorrect. I tried a few other formulas, none of which gave me the results I was looking for. 
     //float newCenter = touchPoint.x; <--- This slides smoothly but always centers view on touch-point (we want to drag from anywhere) 

     NSLog(@"--------------"); 
     NSLog(@"sliderHalfWidth: %f", sliderHalfWidth); 
     NSLog(@"sliderView.center.x: %f", sliderView.center.x); 
     NSLog(@"leftEdge: %f", leftEdge); 
     NSLog(@"distanceToTouchFromEdge: %f", distanceToTouchFromEdge); 
     NSLog(@"touchPoint.x: %f", touchPoint.x); 
     NSLog(@"newCenter: %f", newCenter); 
     NSLog(@"--------------"); 

     sliderView.center = CGPointMake(newCenter, sliderView.frame.size.height/2); 

    } 
} 

어떤 생각?

답변

관련 문제