gpt4 book ai didi

ios - 捏/缩放图像,以触摸为中心

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:04:25 33 4
gpt4 key购买 nike

我可以像这样使用缩放功能:

- (void)twoFingerPinch:(UIPinchGestureRecognizer *)recognizer
{

if([recognizer state] == UIGestureRecognizerStateBegan) {
_lastScale = 1.0;
}

CGFloat scale = 1.0 - (_lastScale - [recognizer scale]);

CGAffineTransform currentTransform = self.imageForEditing.transform;
CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);

[self.imageForEditing setTransform:newTransform];

_lastScale = [recognizer scale];


}

问题是图像根据它的中心缩放,所以如果您先平移图像使其不居中,然后捏/缩放,它不会放大到触摸之间的区域。我认为这一定是一个已解决的常见问题,但我一直未能找到解决方案。

最佳答案

@objc private func pinchHandler(gesture: UIPinchGestureRecognizer) {
if let view = gesture.view {

switch gesture.state {
case .changed:
let pinchCenter = CGPoint(x: gesture.location(in: view).x - view.bounds.midX,
y: gesture.location(in: view).y - view.bounds.midY)
let transform = view.transform.translatedBy(x: pinchCenter.x, y: pinchCenter.y)
.scaledBy(x: gesture.scale, y: gesture.scale)
.translatedBy(x: -pinchCenter.x, y: -pinchCenter.y)
view.transform = transform
gesture.scale = 1
case .ended:
// Nice animation to scale down when releasing the pinch.
// OPTIONAL
UIView.animate(withDuration: 0.2, animations: {
view.transform = CGAffineTransform.identity
})
default:
return
}


}
}

关于ios - 捏/缩放图像,以触摸为中心,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16112140/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com