从iOS tableview创建图像弹出窗口

时间:2014-02-18 14:14:00

标签: ios uitableview uiimage uigesturerecognizer

我想创建一个像Twitter iOS应用程序一样的弹出窗口图像,您可以在其中单击表格视图中的图像,然后全屏显示图像,以便您更好地查看它。

我尝试了以下内容:

         UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleTap:)];  //this is called from the configurecell function

然后我调用的函数是:

-(void)handleTap:(UIGestureRecognizer *)sender
{
CGPoint tapLocation = [sender locationInView:self.tableView];
NSIndexPath *tapIndexPath = [self.tableView indexPathForRowAtPoint:tapLocation];
Location *location = [self.fetchedResultsController objectAtIndexPath:tapIndexPath];
UIImage *overlayImage = [UIImage imageNamed:@"Default.PNG"];  //default.png is just the placeholder for now
UIImageView *overlayImageView = [[UIImageView alloc] initWithImage:overlayImage];
[self.tableView addSubview:overlayImageView];
}

这将导致图像弹出,但仅在拍摄图像的单元格上方。

编辑:

非常感谢投入。我发现我的问题是uiimageview固定在表格视图的顶部,因为CGRectMake是0,0等等(0,0,是左上角)。相反,我们需要将图像视图设置为当前可见区域,如此

-(void)handleTap:(UIGestureRecognizer *)sender
 {
CGPoint tapLocation = [sender locationInView:self.tableView];
NSIndexPath *tapIndexPath = [self.tableView indexPathForRowAtPoint:tapLocation];
CGFloat distanceFromBottom = [self.tableView contentOffset].y;
UIImageView *imview=[[UIImageView alloc] initWithFrame:CGRectMake(0, distanceFromBottom, self.view.frame.size.width, self.view.frame.size.height)];
imview.backgroundColor=[UIColor blackColor];
imview.image = @"Default.PNG";
imview.tag=12345;  //give tag so we can find it and dismiss it
imview.contentMode=UIViewContentModeScaleAspectFit; //make sure image isn't stretched 
[self.view addSubview:imview];  //add the subview
self.tableView.scrollEnabled=NO; //make sure we can't scroll while image is popped out

 //now lets add a gesture recognizer to make sure we can dismiss the pop up uiimageview
 UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissTap:)];
 tapGesture.numberOfTapsRequired = 1;
 tapGesture.cancelsTouchesInView=YES;
 imview.userInteractionEnabled = YES;
 [imview addGestureRecognizer:tapGesture];
 }


-(void)dismissTap:(UIGestureRecognizer *)sender
{
[[self.view viewWithTag:12345]removeFromSuperview];
self.tableView.scrollEnabled=YES;  //re enable scrolling
}

2 个答案:

答案 0 :(得分:0)

喜欢这个

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"PopUP Title" 
                                                message:@"This is pop up window/ Alert" 
                                               delegate:nil 
                                      cancelButtonTitle:@"OK"
                                      otherButtonTitles:nil];

UIImageView *tempImageView=[[UIImageView alloc]initWithFrame:CGRectMake(20,20,50,50)];
tempImageView.image=[UIImage imageNamed:@"abc.png"];

[alert addSubView:tempImageView]

[alert show];

或者您可以从here github

下载

答案 1 :(得分:0)

尝试使用:

-(void)handleTap:(UIGestureRecognizer *)sender
{
CGPoint tapLocation = [sender locationInView:self.tableView];
NSIndexPath *tapIndexPath = [self.tableView indexPathForRowAtPoint:tapLocation];
Location *location = [self.fetchedResultsController objectAtIndexPath:tapIndexPath];
UIImage *overlayImage = [UIImage imageNamed:@"Default.PNG"];  //default.png is just the placeholder for now
UIImageView *overlayImageView = [[UIImageView alloc] initWithImage:overlayImage];
overlayImageView.frame = self.view.frame; // or set it as tableview.frame...set frame as per requirement.
[self.view addSubview:overlayImageView]; /You can also add a button to bring it back also.
}