ユーザーがタップしたときにタイムラインから写真を全画面表示する、Facebook のような iOS アプリを作成したいと思います。
アップデート:
UICollectionView を使用してセルに画像を表示しているので、collectionView:didSelectItemAtIndexPath:
メソッドを使用する必要がありますか? imageView はセル内にあるので、直接フルスクリーンに拡大できますか?
以下にいくつかの画像を添付します。
これはかなり基本的な解決策です。コレクション セルには、UICollectionViewCell contentView の唯一のサブビューとして UIImageView があることを前提としています。
#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];
}];
}
デフォルトではイメージには UserInteraction がないため、設定UITapGestureRecognizer
を忘れないでください。imageView.userInteractionEnabled = YES;
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にあなたのイメージを持っているということです。これは私が現時点で持っているものです。
- (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 を返します。
ここに投稿した webview に少し問題があります (問題が発生した場合に備えて!!)コード。
お役に立てれば!
あなたは正しいです、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
}