我在 UITableviewCell 中有一个 UIImageView。当它被点击时, UIImageView 应该动画以全屏显示。当图像在全屏时被点击时,它应该收缩回原来的位置。
如何做到这一点?
我在 UITableviewCell 中有一个 UIImageView。当它被点击时, UIImageView 应该动画以全屏显示。当图像在全屏时被点击时,它应该收缩回原来的位置。
如何做到这一点?
将手势识别器添加到视图控制器。
将手势识别器添加到您的头文件中
@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;
}
}
@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;
}
}
我最终使用了MHFacebookImageViewer。集成很容易,没有子类化UIImageView
,并且它还具有图像缩放和轻弹关闭。
虽然它需要AFNetworking
(用于从 URL 加载更大的图像),但您可以注释掉一些代码(大约 10 行)来删除这种依赖关系。如果有人需要,我可以发布我的无 AFNetworking 版本。让我知道 :)
一种可能的实现是使用具有UIModalPresentationFullScreen表示样式的模态视图控制器。
刚刚快速完成了一个版本,只需下载并添加到您的项目中:
和用法:
let imageView = GSSimpleImageView(frame: CGRectMake(20, 100, 200, 200))
imageView.image = UIImage(named: "test2.png")
self.view.addSubview(imageView)