我正在构建一个由UITableViewController
语言组成的简单应用程序,当点击特定单元格时,会调出UIPageViewController
来表示所选语言的图像。用户可以滚动图像,一切都按照需要工作。下一步是在UIPageViewController
中构建缩放功能,以便用户可以使用捏合手势放大图像。
我用以下代码实现了这个目标:
- (void)viewDidLoad
{
[super viewDidLoad];
self.leafletImages = [NSMutableArray arrayWithObjects:[[ImageModel alloc] initWithImageName:@"3facts-chinese-page1.jpg"], [[ImageModel alloc] initWithImageName:@"3facts-chinese-page2.jpg"], [[ImageModel alloc] initWithImageName:@"3facts-chinese-page3.jpg"], [[ImageModel alloc] initWithImageName:@"3facts-chinese-page4.jpg"], [[ImageModel alloc] initWithImageName:@"3facts-chinese-page5.jpg"], [[ImageModel alloc] initWithImageName:@"3facts-chinese-page6.jpg"], nil];
// Lots of code for the building up of the UIPageViewController
LeafletImageSizeViewController *imageViewController = [[LeafletImageSizeViewController alloc] init];
imageViewController.model = [_modelArray objectAtIndex:0];
NSArray *viewControllers = [NSArray arrayWithObject:imageViewController];
[self.pageViewController setViewControllers:viewControllers
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:nil];
// Gesture
UIPinchGestureRecognizer *pinchRecognizer = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(pinchDetected:)];
[self.view addGestureRecognizer:pinchRecognizer];
pinchRecognizer.delegate=self;
}
创建图像和大小的类是:
- (void)useThreeFactsSize
CGRect insetFrame;
insetFrame = CGRectMake(310, 70, self.view.frame.size.width-615, self.view.frame.size.height-85);
_imageView = [[UIImageView alloc] initWithFrame:insetFrame];
[_imageView setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[_imageView setImage:[UIImage imageNamed:_model.imageName]];
[[self view] addSubview:_imageView];
[self.view setBackgroundColor:[UIColor clearColor]];
}
pinchDetection方法是:
-(void)pinchDetected:(UIPinchGestureRecognizer *)pinchRecognizer
{
CGFloat scale = pinchRecognizer.scale;
self.pageViewController.view.transform = CGAffineTransformScale(self.pageViewController.view.transform, scale, scale);
pinchRecognizer.scale = 1.0;
}
现在,我可以放大UIPageViewController
的图片而不会出现任何问题,而且效果非常好。
我想做的是两件事:
不允许将图像缩小到原始比例之外
创建双击手势,将图像恢复原状
使用功能1,用户可以放大图像,但也可以完全缩小缩小图像的图像和UIPageViewController
pageIndicators
。没有理由用户应该能够缩小图像,所以我想让用户放大到任何比例,但不要缩小超出图像的原始尺寸在UIPageViewController
的屏幕上。
使用功能2,我想实现一个手势来双击屏幕,并使缩放的图像返回到它的原始比例(如Photos.app)。
更新
参考答案,我更新了问题,以反映我如何处理图像。使用第2点和双击手势,以下代码几乎可以正常工作:
- (void)scrollViewTwoFingerTapped:(UITapGestureRecognizer *)recognizer
{
NSLog(@"Double Tap");
self.pageViewController.view.transform = CGAffineTransformIdentity;
}
它目前正在做的是如果我用捏和平移放大,然后双击,它将图像居中到我点击的位置,所以有时边框正在显示等等,而不是让图像中心到达它应该的位置。
对于第1点:
if (pinchRecognizer.scale > 1) {
CGFloat scale = pinchRecognizer.scale;
self.pageViewController.view.transform = CGAffineTransformScale(self.pageViewController.view.transform, scale, scale);
pinchRecognizer.scale = 1.0;
}
如果我有self.imageview,它不起作用,因为它没有,即使我打电话给设置大小的类,它也是零。
我怀疑我的代码有很多问题!
作为参考,我已经开始使用:
- (void)panGestureDetected:(UIPanGestureRecognizer *)recognizer
{
UIGestureRecognizerState state = [recognizer state];
if (state == UIGestureRecognizerStateBegan || state == UIGestureRecognizerStateChanged)
{
CGPoint translation = [recognizer translationInView:recognizer.view];
[recognizer.view setTransform:CGAffineTransformTranslate(recognizer.view.transform, translation.x, translation.y)];
[recognizer setTranslation:CGPointZero inView:recognizer.view];
}
else if(state==UIGestureRecognizerStateEnded){
UIView *imageView = recognizer.view;
UIView *container = imageView.superview;
CGFloat targetX = CGRectGetMinX(imageView.frame);
CGFloat targetY = CGRectGetMinY(imageView.frame);
if(targetX>0){
// targetX = 0;
}else if(CGRectGetMaxX(imageView.frame)<CGRectGetWidth(container.bounds)){
targetX = CGRectGetWidth(container.bounds)-CGRectGetWidth(imageView.frame);
}
if(targetY>0){
// targetY = 0;
}else if(CGRectGetMaxY(imageView.frame)<CGRectGetHeight(container.bounds)){
// targetY = CGRectGetHeight(container.bounds)-CGRectGetHeight(imageView.frame);
}
// imageView.frame = CGRectMake(targetX, targetY, CGRectGetWidth(imageView.frame), CGRectGetHeight(imageView.frame));
[UIView animateWithDuration:0.3 animations:^{
imageView.frame = CGRectMake(targetX, targetY, CGRectGetWidth(imageView.frame), CGRectGetHeight(imageView.frame));
}];
}
}
此刻目前运作良好,但肯定与其他一切都存在冲突。
我非常感谢任何正确方向的指导。
答案 0 :(得分:0)
1。)为了不允许将图像缩小到原始比例之外,首先只需检查您要将其设置为的比例是否大于1。如果它小于1,您不希望重新缩放图像,因为这意味着它会变小。所以......
@IBAction func doPinch(sender: UIPinchGestureRecognizer) {
if sender.scale > 1 {
let transform = CGAffineTransformMakeScale(sender.scale, sender.scale)
imageView.transform = transform
}
}
2。)您似乎正在更改视图的框架以尝试更改它的比例,但您从未首先自己调整视图的框架。您正在调整视图的transform
。这意味着为了将其恢复为原始大小,您必须删除放在其上的任何变换。要做到这一点,你要回到它的身份。所以......
@IBAction func doDoubleTap(sender: UITapGestureRecognizer) {
imageView.transform = CGAffineTransformIdentity
}
我的代码示例在Swift中,但您应该可以自己将其调整为Objective-C。此外,您似乎正在调整整个页面视图本身的变换/比例。我建议你只改变图像视图的比例。这更有意义,因为这实际上是你想要放大的。