将 PinchRecognizer Objective-C 代码转换为 Swift
Convert PinchRecognizer Objective-C code to Swift
我正在尝试将 Objective-C 代码翻译成 Swift,谁能帮忙。我被困在一条线上。
Objective-C
-(void)pinchDetected:(UIPinchGestureRecognizer *)pinchRecognizer
{
if([pinchRecognizer state] == UIGestureRecognizerStateBegan) {
// Reset the last scale, necessary if there are multiple objects with different scales
lastScale = [pinchRecognizer scale];
}
if ([pinchRecognizer state] == UIGestureRecognizerStateBegan ||[pinchRecognizer state] == UIGestureRecognizerStateChanged)
{
###I NEED TO CONVERT THIS LINE
CGFloat currentScale = [[[pinchRecognizer view].layer valueForKeyPath:@"transform.scale"] floatValue];
const CGFloat maxScale = 1.5;
const CGFloat minScale = 1.0;
CGFloat newScale = 1 - (lastScale - [pinchRecognizer scale]);
newScale = MIN(newScale, maxScale / currentScale);
newScale = MAX(newScale, minScale / currentScale);
thePageViewController.view.transform = CGAffineTransformScale(thePageViewController.view.transform, newScale, newScale);
lastScale = [pinchRecognizer scale]; // Store the previous scale factor for the next pinch gesture call
}
}
Swift
func pinchDetected(pinchRecognizer: UIPinchGestureRecognizer) {
var lastScale = pinchRecognizer.scale
if(pinchRecognizer.state == UIGestureRecognizerState.Began) {
// Reset the last scale, necessary if there are multiple objects with different scales
lastScale = pinchRecognizer.scale
}
if (pinchRecognizer.state == UIGestureRecognizerState.Began || pinchRecognizer.state == UIGestureRecognizerState.Changed)
{
###HOW DO I CONVERT THIS LINE TO SWIFT?
var currentScale: CGFloat = [[[pinchRecognizer.view].layer valueForKeyPath:@"transform.scale"] floatValue];
let maxScale: CGFloat = 1.5;
let minScale: CGFloat = 1.0;
var newScale: CGFloat = 1 - (lastScale - pinchRecognizer.scale)
newScale = min(newScale, maxScale / currentScale);
newScale = max(newScale, minScale / currentScale);
self.view.transform = CGAffineTransformScale(self.view.transform, newScale, newScale);
lastScale = pinchRecognizer.scale // Store the previous scale factor for the next pinch gesture call
}
}
你说的转换的Line可以这样操作。这对我有用。
var curScale = pinchRecognizer.view!.layer.valueForKeyPath("transform.scale")!.floatValue
我正在尝试将 Objective-C 代码翻译成 Swift,谁能帮忙。我被困在一条线上。
Objective-C
-(void)pinchDetected:(UIPinchGestureRecognizer *)pinchRecognizer
{
if([pinchRecognizer state] == UIGestureRecognizerStateBegan) {
// Reset the last scale, necessary if there are multiple objects with different scales
lastScale = [pinchRecognizer scale];
}
if ([pinchRecognizer state] == UIGestureRecognizerStateBegan ||[pinchRecognizer state] == UIGestureRecognizerStateChanged)
{
###I NEED TO CONVERT THIS LINE
CGFloat currentScale = [[[pinchRecognizer view].layer valueForKeyPath:@"transform.scale"] floatValue];
const CGFloat maxScale = 1.5;
const CGFloat minScale = 1.0;
CGFloat newScale = 1 - (lastScale - [pinchRecognizer scale]);
newScale = MIN(newScale, maxScale / currentScale);
newScale = MAX(newScale, minScale / currentScale);
thePageViewController.view.transform = CGAffineTransformScale(thePageViewController.view.transform, newScale, newScale);
lastScale = [pinchRecognizer scale]; // Store the previous scale factor for the next pinch gesture call
}
}
Swift
func pinchDetected(pinchRecognizer: UIPinchGestureRecognizer) {
var lastScale = pinchRecognizer.scale
if(pinchRecognizer.state == UIGestureRecognizerState.Began) {
// Reset the last scale, necessary if there are multiple objects with different scales
lastScale = pinchRecognizer.scale
}
if (pinchRecognizer.state == UIGestureRecognizerState.Began || pinchRecognizer.state == UIGestureRecognizerState.Changed)
{
###HOW DO I CONVERT THIS LINE TO SWIFT?
var currentScale: CGFloat = [[[pinchRecognizer.view].layer valueForKeyPath:@"transform.scale"] floatValue];
let maxScale: CGFloat = 1.5;
let minScale: CGFloat = 1.0;
var newScale: CGFloat = 1 - (lastScale - pinchRecognizer.scale)
newScale = min(newScale, maxScale / currentScale);
newScale = max(newScale, minScale / currentScale);
self.view.transform = CGAffineTransformScale(self.view.transform, newScale, newScale);
lastScale = pinchRecognizer.scale // Store the previous scale factor for the next pinch gesture call
}
}
你说的转换的Line可以这样操作。这对我有用。
var curScale = pinchRecognizer.view!.layer.valueForKeyPath("transform.scale")!.floatValue