[英]How do I make an image expand to full screen when touched?
我想制作一个类似Facebook的iOS应用程序,当用户点击它时,它会从时间轴全屏显示。
更新:
我正在使用UICollectionView在单元格中显示图像,所以我应该使用collectionView:didSelectItemAtIndexPath:
方法? 并且imageView在单元格中,所以我还能直接扩展到全屏吗?
附上几张图片:
这是一个相当基本的解决方案。 它假定您的集合单元格具有UIImageView作为UICollectionViewCell contentView的唯一子视图。
#import <objc/runtime.h> // for objc_setAssociatedObject / objc_getAssociatedObject
...
- (void) collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
UICollectionViewCell* cell = [collectionView cellForItemAtIndexPath:indexPath];
UIImageView* iv = cell.contentView.subviews.lastObject;
UIImageView* ivExpand = [[UIImageView alloc] initWithImage: iv.image];
ivExpand.contentMode = iv.contentMode;
ivExpand.frame = [self.view convertRect: iv.frame fromView: iv.superview];
ivExpand.userInteractionEnabled = YES;
ivExpand.clipsToBounds = YES;
objc_setAssociatedObject( ivExpand,
"original_frame",
[NSValue valueWithCGRect: ivExpand.frame],
OBJC_ASSOCIATION_RETAIN);
[UIView transitionWithView: self.view
duration: 1.0
options: UIViewAnimationOptionAllowAnimatedContent
animations:^{
[self.view addSubview: ivExpand];
ivExpand.frame = self.view.bounds;
} completion:^(BOOL finished) {
UITapGestureRecognizer* tgr = [[UITapGestureRecognizer alloc] initWithTarget: self action: @selector( onTap: )];
[ivExpand addGestureRecognizer: tgr];
}];
}
- (void) onTap: (UITapGestureRecognizer*) tgr
{
[UIView animateWithDuration: 1.0
animations:^{
tgr.view.frame = [objc_getAssociatedObject( tgr.view,
"original_frame" ) CGRectValue];
} completion:^(BOOL finished) {
[tgr.view removeFromSuperview];
}];
}
使用UITapGestureRecognizer
,不要忘记设置UITapGestureRecognizer
imageView.userInteractionEnabled = YES;
因为默认情况下图像没有UserInteraction。
UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
[singleTap setNumberOfTapsRequired:1];
[imageView addGestureRecognizer:singleTap];
-(void)handleSingleTap:(id)sender {
// push you view here
//code for full screen image
}
我所理解的是你在UIWebView中拥有你的图像这就是我现在所拥有的,将UIWebViews Delegate设置为self然后添加:
- (BOOL)webView:(UIWebView*)webView shouldStartLoadWithRequest:(NSURLRequest*)request navigationType:(UIWebViewNavigationType)navigationType {
if (navigationType == UIWebViewNavigationTypeLinkClicked) {
NSURL *URL = [request URL];
ImageViewer *imageView = [[[ImageViewer alloc] initWithNibName:@"ImageViewer" bundle:nil] autorelease];
imageView.imageURL = URL;
[self presentModalViewController:imageView animated:YES];
return NO;
} else {
return YES;
}
}
只需像往常一样使图像成为html中的链接。 如果你有其他你不想在模型中加载的链接,那么你可以修改代码来检测被按下的链接是否会转到图像,否则只返回YES;
我在我的网页浏览中遇到了一些问题(只是因为你遇到了一些问题!!) 代码。
希望这可以帮助!
你是对的,使用UICollectionView的委托方法:
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
//get UIImage from source
//show (add subView) UIImageView with full screen frame
//add gesture for double tap on which remove UIImageView
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.