2013-11-04 1 views
0

enter image description here UIPanGestureRecognizer 클래스를 사용하여 y 축을 위아래로 드래그하는 UIButton이 있습니다. 나는 수직 기능을 위해 UISlider 대신 이것을 사용하고있다. 0에서 1 사이의 모든 float 값을 캡처하려면 UISlider처럼 단추를 위아래로 드래그하여 최소값 0에서 최대 1로 제한하십시오.UIPanGestureRecognizer를 사용하여 y 값에서 0과 1 사이의 float 값을 얻는 방법

현재 코드 isn ' 정확한 값을 포착합니다. 나는이 문제를 해결할 수있는 방법 : 나는 비슷한 달성하기 위해 locationOfTouch (하지translationInView)과 제스처의 다른 state에 의존

- (void)wasDragged:(UIPanGestureRecognizer *)recognizer { 

    CGPoint translation = [recognizer translationInView:self.view]; 

    CGPoint newCenter = CGPointMake(recognizer.view.center.x,recognizer.view.center.y + translation.y); 

    //range is 139 pixels 

    //i tried dividing 1/139 to get the increment using pixels 

    if(newCenter.y >= 126 && newCenter.y <= 265) 
    { 
     currentMainVolValue = currentMainVolValue - 0.00714285714286; 
     recognizer.view.center = newCenter; 
     [recognizer setTranslation:CGPointZero inView:self.view]; 
    } 
} 
+1

당신은 최소 수동 UI 슬라이더의 최대 값은, 다음 분할을 설정할 수 준다 당신이 원하는 감도에 따라 플로트를 얻으십시오. – ManicMonkOnMac

답변

1

126 가장 작은 y 값이고 265가 최대라고 가정. 현재 변환에서 가장 작은 Y를 빼기 다음 범위로 결과를 분할해야

CGFloat minY = 126; 
CGFloat range = 256 - minY; 

return 1 - ((yTranslation - minY)/range); 

yTranslation = 256 then value = 0 
yTranslation = 126 then value = 1 
yTranslation = 191 then value = 0.5 
+0

완벽 고맙습니다 –

0

.

필자의 경우,이 하위 뷰 안의 고정 된 단추 (UIPanGestureRecognizer에 등록 된 단추)를 사용하여 하위보기를 이동해야했습니다. 일단 제스처가 끝나면, 내 시야가 화면의 왼쪽 벽에 다시 스냅됩니다 (y 축 이동).

- (void)moveViewWithGestureRecognizer:(UIPanGestureRecognizer *)gestureRecognizer 
{ 
    // Need to make below calculation for other orientations. Until then this is not allowed. 
    if (self.interfaceOrientation != UIInterfaceOrientationPortrait) return; 
    static CGFloat initialCenterPointX = 0; 

    const CGPoint deltaPoint = CGPointMake(self.view.bounds.size.width/2 - gestureRecognizer.view.superview.center.x, self.view.bounds.size.height/2 - gestureRecognizer.view.superview.center.y); 

    if (gestureRecognizer.state == UIGestureRecognizerStateBegan) { 
     initialCenterPointX = self.view.center.x; 
    } 
    else if (gestureRecognizer.state == UIGestureRecognizerStateChanged) { 
     CGPoint location = [gestureRecognizer locationOfTouch:0 inView:self.view]; 

     NSLog(@"gestureRecognizer.view = %@ location = %f, %f", gestureRecognizer.view, location.x, location.y); 
     CGFloat proposedCenterPointX = self.view.frame.origin.x + location.x; 
     CGFloat proposedCenterPointY = self.view.frame.origin.y + deltaPoint.y + location.y; 

     CGPoint newCenterLocation = CGPointZero; 

     newCenterLocation.x = MAX(proposedCenterPointX, self.view.bounds.size.width/2); 
     newCenterLocation.y = MAX(proposedCenterPointY, self.view.bounds.size.height/2); 

     self.view.center = newCenterLocation; 
    } 
    else { 
     [UIView animateWithDuration:0.2 animations:^{ 
      self.view.center = CGPointMake(initialCenterPointX, self.view.center.y); 
     }]; 
    } 
} 
관련 문제