使用
UIPinchGestureRecognizer 手势可以放大或缩小UIImageView视图。放大或缩小的值是根据
UIPinchGestureRecognizer 的scale决定。这个值在手势的生命周期内是一个时间点的值。可以假设为开始时这个值是1,缩放率为1,很好理解。
在手势begin的,将UIImageView视图的transform记录下来,作为初始值。在手势的changed过程中,每一个changed时候获取的scale值都是和begin时的值的比率。在手势结束时,将scale的值也记录下来。
还有一个原因,这个pinch手势会多次执行,要知道从第一次缩放到最后退出,总的缩放比率是多大就得这样操作。
_lastPhotoScale = _lastPhotoScale *gesture.scale ;
基本的数学概率,也很好理解。
这里缩放的仅仅是 UIImageView视图,对于视图的image是没有任何改变的。因此,既然 UIImageView视图的size很小了,但图片看上去还是非常清晰,图片的像素还是那么高呀。
如果需要将 UIImage也做缩放的话,在UIImage中去实现。在 UIImage真的缩小了以后,你发现,图片不清晰了。这是正常的,因为图片的体积真的小了。
在手势begin的,将UIImageView视图的transform记录下来,作为初始值。在手势的changed过程中,每一个changed时候获取的scale值都是和begin时的值的比率。在手势结束时,将scale的值也记录下来。
还有一个原因,这个pinch手势会多次执行,要知道从第一次缩放到最后退出,总的缩放比率是多大就得这样操作。
_lastPhotoScale = _lastPhotoScale *gesture.scale ;
基本的数学概率,也很好理解。
这里缩放的仅仅是 UIImageView视图,对于视图的image是没有任何改变的。因此,既然 UIImageView视图的size很小了,但图片看上去还是非常清晰,图片的像素还是那么高呀。
如果需要将 UIImage也做缩放的话,在UIImage中去实现。在 UIImage真的缩小了以后,你发现,图片不清晰了。这是正常的,因为图片的体积真的小了。
- (void)scalePhoto:(UIPinchGestureRecognizer *)gesture
{
// GTMLoggerDebug(@"scale is %f",gesture.scale);
if (gesture.state ==UIGestureRecognizerStateBegan) {
currentTransform =_photoView.transform;
}
if (gesture.state ==UIGestureRecognizerStateChanged) {
CGAffineTransform tr =CGAffineTransformScale(currentTransform, gesture.scale, gesture.scale);
_photoView.transform = tr;
_photoView.frame =CGRectMake(0,0, _photoView.frame.size.width,_photoView.frame.size.height);
NSLog(@"ing:_lastPhotoScale is %f,scale is %f,frame is %@",_lastPhotoScale, gesture.scale,NSStringFromCGSize(_photoView.frame.size));
}
// 当手指离开屏幕时,将lastscale设置为1.0
if ((gesture.state ==UIGestureRecognizerStateEnded) || (gesture.state ==UIGestureRecognizerStateCancelled)) {
_lastPhotoScale =_lastPhotoScale*gesture.scale;
NSLog(@"end:_lastPhotoScale is %f,scale is %f,frame is %@",_lastPhotoScale, gesture.scale,NSStringFromCGSize(_photoView.frame.size));
GTMLoggerDebug(@"_photoImage %p size is %@,_lastPhotoScale is %f",_photoImage, NSStringFromCGSize(_photoImage.size),_lastPhotoScale);
GTMLoggerDebug(@"_photoView %p frame is %@",_photoView.image,NSStringFromCGRect(_photoView.frame));
}
}