我在UITableviewCell中有一个UIImageView。当点击它时,UIImageView应该动画显示全屏。当全屏显示图像时,它应缩回原始位置。
如何实现这一目标?
答案 0 :(得分:28)
将手势识别器添加到视图控制器。
将手势识别器添加到头文件
@interface viewController : UIViewController <UIGestureRecognizerDelegate>{
UITapGestureRecognizer *tap;
BOOL isFullScreen;
CGRect prevFrame;
}
在viewDidLoad中添加以下内容:
isFullScreen = false;
tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imgToFullScreen)];
tap.delegate = self;
添加以下委托方法:
- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch;
{
BOOL shouldReceiveTouch = YES;
if (gestureRecognizer == tap) {
shouldReceiveTouch = (touch.view == yourImageView);
}
return shouldReceiveTouch;
}
现在您只需要实现imgToFullScreen方法。 确保你使用isFullScreen Bool(全屏如果是假的,如果它是真的则回到旧的大小)
imgToFullScreen方法取决于您希望如何使图像成为全屏。 一种方法是: (这是未经测试但应该有效)
-(void)imgToFullScreen{
if (!isFullScreen) {
[UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
//save previous frame
prevFrame = yourImageView.frame;
[yourImageView setFrame:[[UIScreen mainScreen] bounds]];
}completion:^(BOOL finished){
isFullScreen = true;
}];
return;
} else {
[UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
[yourImageView setFrame:prevFrame];
}completion:^(BOOL finished){
isFullScreen = false;
}];
return;
}
}
答案 1 :(得分:6)
来自@ AzzUrr1的代码,小错误更正(括号)和tapper实现略有不同。
为我工作。现在用scrollView实现它会很棒,如果图片更大,用户可以放大/缩小.. 有什么建议吗?
#import <UIKit/UIKit.h>
@interface ViewController : UIViewController <UIGestureRecognizerDelegate>{
UITapGestureRecognizer *tap;
BOOL isFullScreen;
CGRect prevFrame;
}
@property (nonatomic, strong) UIImageView *imageView;
@end
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
isFullScreen = FALSE;
tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imgToFullScreen)];
tap.delegate = self;
self.view.backgroundColor = [UIColor purpleColor];
_imageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 300, 200)];
_imageView.contentMode = UIViewContentModeScaleAspectFill;
[_imageView setClipsToBounds:YES];
_imageView.userInteractionEnabled = YES;
_imageView.image = [UIImage imageNamed:@"Muppetshow-2.png"];
UITapGestureRecognizer *tapper = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imgToFullScreen:)];
tapper.numberOfTapsRequired = 1;
[_imageView addGestureRecognizer:tapper];
[self.view addSubview:_imageView];
}
-(void)imgToFullScreen:(UITapGestureRecognizer*)sender {
if (!isFullScreen) {
[UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
//save previous frame
prevFrame = _imageView.frame;
[_imageView setFrame:[[UIScreen mainScreen] bounds]];
}completion:^(BOOL finished){
isFullScreen = TRUE;
}];
return;
}
else{
[UIView animateWithDuration:0.5 delay:0 options:0 animations:^{
[_imageView setFrame:prevFrame];
}completion:^(BOOL finished){
isFullScreen = FALSE;
}];
return;
}
}
答案 2 :(得分:2)
我最终使用MHFacebookImageViewer。集成很简单,没有子类化UIImageView
,它还具有图像缩放和轻弹消除功能。
虽然它需要AFNetworking
(用于从URL加载更大的图像),但您可以注释掉一些代码(大约10行)以删除此依赖项。如果有人需要,我可以发布我的AFNetworking免费版本。让我知道:))
答案 3 :(得分:1)
一种可能的实现方式是使用具有UIModalPresentationFullScreen演示风格的模态视图控制器。
答案 4 :(得分:0)
刚刚在swift中完成了一个版本,只需下载并添加到您的项目中:
用法:
let imageView = GSSimpleImageView(frame: CGRectMake(20, 100, 200, 200))
imageView.image = UIImage(named: "test2.png")
self.view.addSubview(imageView)