2016-10-17 3 views
1

중단이 내 코드입니다UIPanGestureRecognizer이 시작 사이의 길이를 얻을

func respondsToPenGesture(sender: UIPanGestureRecognizer) { 
    let startLocation : CGPoint 
    if (sender.state == UIGestureRecognizerState.Began) { 
     startLocation = sender.locationInView(self.newEffectView) 
    } 
    else if (sender.state == UIGestureRecognizerState.Ended) { 

     let stopLocation = sender.locationInView(self.newEffectView) 

     let dx = stopLocation.x - startLocation.x; 
     let dy = stopLocation.y - startLocation.y; 
     let distance = sqrt(dx*dx + dy*dy); 
     NSLog("Distance: %f", distance); 

    } 
} 

나는이 길이를 사용하려면, 그것은 오류 보여줍니다 : 정수 'startLocation'은 초기화되기 전에 사용되었습니다.

내가 그것을 변경하는 경우 :

func respondsToPenGesture(sender: UIPanGestureRecognizer) { 
    //let startLocation : CGPoint 
    if (sender.state == UIGestureRecognizerState.Began) { 
     let startLocation = sender.locationInView(self.newEffectView) 
    } 
    else if (sender.state == UIGestureRecognizerState.Ended) { 

     let stopLocation = sender.locationInView(self.newEffectView) 

     let dx = stopLocation.x - startLocation.x; 
     let dy = stopLocation.y - startLocation.y; 
     let distance = sqrt(dx*dx + dy*dy); 
     NSLog("Distance: %f", distance); 
    } 
} 

이 표시됩니다 : 해결되지 않은 식별자의 사용 'startLocation'. (P.s : 스위치와 케이스를 사용하여 이미 시도했지만 동일한 문제가 있음) 누군가이 문제를 해결할 수 있습니까? Thx 사전에.

답변

1

제스처가 업데이트 될 때마다 handlePanRecognizer()이 호출되므로 제스처가 끝나면 핸들러 메서드의 다른 호출에서 설정되었으므로 startLocation이 없습니다. 이제

var startPanLocation: CGPoint? 

.began 경우를 설정하고 .ended 경우 거리를 계산하는 데 사용 :

당신은 선택적인 속성으로 마지막 위치를 저장한다

func handlePanRecognizer(panRecognizer: UIPanGestureRecognizer) { 
    let currentLocation = panRecognizer.location(in: self.newEffectView) 

    switch panRecognizer.state { 
    case .began: 
     startPanLocation = currentLocation 
    case .ended: 
     let dx = currentLocation.x - startPanLocation.x; 
     let dy = currentLocation.y - startPanLocation.y; 
     let distance = sqrt(dx*dx + dy*dy); 
     startPanLocation = nil 
    default: break 
    } 
} 

, BTW 당신은 ​​사용할 수 없습니다 변수가 정의되었지만 초기화되지 않은 경우 :

let startLocation : CGPoint  //defined 
let startLocation = CGPoint.zero //initialized 
+1

THX :-) 도움이

class MyClass { var startLocation = CGPoint.zero func respondsToPenGesture(sender: UIPanGestureRecognizer) { if (sender.state == UIGestureRecognizerState.Began) { startLocation = sender.locationInView(self.newEffectView) } else if (sender.state == UIGestureRecognizerState.Ended) { let stopLocation = sender.locationInView(self.newEffectView) let dx = stopLocation.x - startLocation.x; let dy = stopLocation.y - startLocation.y; let distance = sqrt(dx*dx + dy*dy); NSLog("Distance: %f", distance); } } } 

희망, 사람이 내가 통해 UR 대답하고 XmasRights의 답을 결합은, 마지막으로 내가 원하는 것을 얻을, 많은 도움이! – CizooDev

+0

@CizooDev 기꺼이 도와 드리겠습니다.그들이 도움이된다면 당신은 둘 다 대답을 upvote 할 수있다. – alexburtnik

2

세 가지 문제가 있습니다.

먼저 letconst 변수를 정의하는 데 사용되거나 변수가 생성되면 변경되지 않습니다. startLocation 값을 빈 CGPoint으로 만든 후에 값을 변경하려고 시도했기 때문에 첫 번째 코드 비트가 실패합니다. letvar으로 변경하면 문제가되지 않습니다.

둘째, 다음 코드 블록에 범위 지정 문제가 있습니다. { }을 사용할 때마다 범위 외부의 모든 변수에 액세스 할 수있는 새 범위를 만들고 그 안에 새 변수를 만들 수 있습니다. 해당 스코프가 닫는 중괄호로 끝나면 let startLocation을 포함하여 내부에서 생성 된 모든 것이 파괴되므로 else if 범위에서 볼 수 없습니다.

마지막으로 범위 지정 문제는 전체적으로이 함수에도 적용됩니다. respondsToPanGesture(sender:) 메서드를 호출 할 때마다 새 startLocation이 만들어지고 모든 이전 인스턴스가 삭제됩니다. 호출간에이 값을 유지하려면 클래스 자체에 startLocation을 만들고 함수 내에서 업데이트해야합니다.

+0

THX, 나는 ur 대답과 alexburtnik의 대답을 결합하여 마침내 내가 원하는 것을 얻는다. – CizooDev

관련 문제