我有一个附加到imageview的捏手势识别器,我从中使用捏来放大和最小化照片。下面是我在委托方法中使用的代码:
- (void)scale:(UIPinchGestureRecognizer *)sender
{
if([sender state] == UIGestureRecognizerStateBegan)
{
lastScale = [sender scale];
}
if ([sender state] == UIGestureRecognizerStateBegan ||
[sender state] == UIGestureRecognizerStateChanged)
{
CGFloat currentScale = [[[sender view].layer valueForKeyPath:@"transform.scale"] floatValue];
CGFloat newScale = 1 - (lastScale - [sender scale]) * (UIComicImageViewPinchSpeed);
newScale = MIN(newScale, minScale / currentScale);
newScale = MAX(newScale, maxScale / currentScale);
CGAffineTransform transform = CGAffineTransformScale([[sender view] transform], newScale, newScale);
[sender view].transform = transform;
lastScale = [sender scale];
}
}
在实际执行转换之前,我需要确定imageview框架的新中心的位置。反正有没有确定这个?基本上,我正试图停止缩放,如果它要将图像移出屏幕或靠近它。
更新
感谢下面的Robin建议找出转换帧的方法。我现在遇到的问题是在执行变换后帧变得无效,我需要跟踪最近的帧以便找出图像的边界。显然,我可以手动执行此操作并将其存储在实例变量中,但想知道是否有更“优雅”的方法来实现此目的?
答案 0 :(得分:2)
像这样使用CGRectApplyAffineTransform:
CGRect currentFrame = ....;
CGRect newFrame = CGRectApplyAffineTransform(currentFrame, transform);
// Then test if newFrame is within the limits you want