아이폰 OS 10.2 스위프트 3.0제한 UIPinchGestureRecognizer 줌 레벨 스위프트 3.0
에 목표 C. SO 포스터로 수정 된 코드!
- (void)handlePinchGesture:(UIPinchGestureRecognizer *)gestureRecognizer {
if([gestureRecognizer state] == UIGestureRecognizerStateBegan) {
// Reset the last scale, necessary if there are multiple objects with different scales
lastScale = [gestureRecognizer scale];
}
if ([gestureRecognizer state] == UIGestureRecognizerStateBegan ||
[gestureRecognizer state] == UIGestureRecognizerStateChanged) {
CGFloat currentScale = [[[gestureRecognizer view].layer valueForKeyPath:@"transform.scale"] floatValue];
// Constants to adjust the max/min values of zoom
const CGFloat kMaxScale = 2.0;
const CGFloat kMinScale = 1.0;
CGFloat newScale = 1 - (lastScale - [gestureRecognizer scale]); // new scale is in the range (0-1)
newScale = MIN(newScale, kMaxScale/currentScale);
newScale = MAX(newScale, kMinScale/currentScale);
CGAffineTransform transform = CGAffineTransformScale([[gestureRecognizer view] transform], newScale, newScale);
[gestureRecognizer view].transform = transform;
lastScale = [gestureRecognizer scale]; // Store the previous scale factor for the next pinch gesture call
}
}
http://paulsolt.com/blog/2011/03/limiting-uipinchgesturerecognizer-zoom-levels
샘플 코드는 거의 다하지만, 스위프트 3.0 여기에 사용되는 CATransform 키에 대한 참조를 찾을 수 없습니다. 일반적으로 그들은 동일한 보장은 없습니다 말하기 때문에 내 코드 ...
if sender.state == .began {
// Reset the last scale, necessary if there are multiple objects with different scales
lastScale = sender.scale
}
if sender.state == .began || sender.state == .changed {
// UPDATED
currentScaleX = self.image2P.transform.scaleX
currentScaleY = self.image2P.transform.scaleY
self.image2P.transform = self.image2P.transform.scaledBy(x: 1.1, y: 1.1)
// Constants to adjust the max/min values of zoom
let kMaxScale:CGFloat = 2.0;
let kMinScale:CGFloat = 1.0;
var newScale = 1 - (lastScale - sender.scale) // new scale is in the range (0-1)
newScale = min(newScale, kMaxScale/currentScaleX)
newScale = max(newScale, kMinScale/currentScaleY)
self.image2P.transform = self.image2P.transform.scaledBy(x: newScale, y: newScale)
lastScale = sender.scale // Store the previous scale factor for the next pinch gesture call
}
은'value()'대신'self.image2P.layer.transform.scale'을 사용합니다. –
슬프게도 Sachin이 존재하지 않는 것 같습니다 :) – user3069232
사용할 수없는 것. –