在IOS中点击手势显示图像更大

时间:2017-09-25 11:34:18

标签: ios objective-c

我使用第三方lib在滑块中显示图像数组。阵列中的图像来自服务器,现在我想在点击手势上以更大的图像视图显示图像。当图像用户点击它的任何人应该在大图像视图中显示。我尝试了一些代码,但它没有用。我的代码是,

  int i=0;

    tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(expandImage:)];
    tap.numberOfTapsRequired = 1;
    [_ImageView setUserInteractionEnabled:YES];
    [_ImageView addGestureRecognizer:tap];
    tap = [_imagesData objectAtIndex:i];
    NSLog(@"TTT %@",tap);

    [self.view addSubview:_fullImage];

方法是,

-(void)expandImage:(UITapGestureRecognizer*)recogniser
{
    [self.view addSubview:_fullImage];

    UIButton *closeButton = [[UIButton alloc]init];
    [closeButton setTitle:@"Close" forState:UIControlStateNormal];
   // [closeButton addTarget:self action:@selector(Closetab) forControlEvents:UIControlEventTouchUpInside];
    [_fullImage addSubview:closeButton];

    UIImageView *photoView = [[UIImageView alloc]init];
    [photoView setBackgroundColor:[UIColor blueColor]];
    [_fullImage addSubview:photoView];
   // photoView.accessibilityIdentifier = @"nature2.png";

    [_fullImage addSubview:_fullImage];

}

1 个答案:

答案 0 :(得分:0)

这里我附上sample project,自定义

<强>步骤1

创建一个全屏视图以预览图像(使用一个UIView,一个关闭btn,一个imageview)

@interface ViewController () <UIScrollViewDelegate, TAPageControlDelegate>
{
NSTimer *timer;
NSInteger index;
//
IBOutlet UIImageView *imgfull;
IBOutlet UIView *fullimagevie;
IBOutlet UIButton *btnClose;
}

<强>步骤2

添加用于点按图像事件的手势,并将重复视图的帧设置为viewcontroller的底部

- (void)viewDidLoad
{
[super viewDidLoad];

 [self setviewframe:self.view.frame.size.height + 10];
self.imagesData = @[@"image1.jpg", @"image2.jpg", @"image3.jpg"];
for(int i=0; i<self.imagesData.count;i++){
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame) * i, 0, CGRectGetWidth(self.view.frame), CGRectGetHeight(self.scrollView.frame))];
    imageView.contentMode = UIViewContentModeScaleAspectFill;
    imageView.image = [UIImage imageNamed:[self.imagesData objectAtIndex:i]];

   UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(expandImage:)];
    tap.numberOfTapsRequired = 1;
    tap.view.tag = i;
    [imageView setUserInteractionEnabled:YES];
    [imageView addGestureRecognizer:tap];

    [self.scrollView addSubview:imageView];
}

<强>步骤3

处理图像点按以预览图像

-(void)expandImage:(UITapGestureRecognizer*)recogniser
 {
imgfull.image = [UIImage imageNamed:[self.imagesData objectAtIndex:recogniser.view.tag]];
[UIView transitionWithView:fullimagevie
                  duration:0.4
                   options:UIViewAnimationOptionTransitionCrossDissolve
                animations:^{
                   [self setviewframe:0];
                }
                completion:NULL];

}

<强>步骤4

点按关闭按钮

,关闭较大的图片视图
- (IBAction)btnClose:(UIButton *)sender {

[UIView transitionWithView:fullimagevie
                  duration:0.4
                   options:UIViewAnimationOptionTransitionCrossDissolve
                animations:^{
                    [self setviewframe:self.view.frame.size.height + 10];
                }
                completion:NULL];

}

<强> STEP5

创建为更大的imageview设置框架的常用方法

-(void)setviewframe:(CGFloat)coordinate{
CGRect modifyframe = fullimagevie.frame;
modifyframe.origin.y = coordinate;
fullimagevie.frame = modifyframe;
}