UIView-tag循环图片点击图片

    //添加初始图片

    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 20, 250, 300)];

    imgView.center = CGPointMake(160, 240);

    imgView.image = [UIImage imageNamed:@"1.jpg"];

    imgView.tag = 101;

    [self.window addSubview:imgView];

    [imgView release];

    

    /*********************************/

    //默认UIImageView无法接收手势

    imgView.userInteractionEnabled = YES;

    /*********************************/

    /*

     手势:点击  拖拽  缩放  长按  方向  旋转

     */

    //点击手势

    UITapGestureRecognizer *tap =[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(myTap:)];

    // 默认是单击手势

    // 双击手势

    tap.numberOfTapsRequired = 2;

    //添加手势

    [imgView addGestureRecognizer:tap];

    

    //添加标题

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(35, 60, 250, 30)

                      ];

    label.textAlignment = NSTextAlignmentJustified;

    label.tag = 100;

    [self.window addSubview:label];

    label.text = @"1.jpg";

    

    NSArray *arr = @[@"上一张",@"下一张"];

    [label release];

    for (int i =0; i<2; i++) {

        UIButton *b = [UIButton buttonWithType:UIButtonTypeCustom];

        b.frame = CGRectMake(35 +150*i, 400, 100, 30);

        [b setTitle: arr[i] forState:UIControlStateNormal];

        [b setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];

        [b addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

        b.tag = 100 + i + 2;

        [self.window addSubview:b];

    }

    

    return YES;

}

/*

 tag

    100:标题

    101:图片

    102:上一张

    103:下一张

 */

int curPage = 1;

-(void)click:(UIButton *)b

{

    //通过tag来获取window上得UILabelUIView

    UILabel *label = (UILabel *)[self.window viewWithTag:100];

    UIImageView *imgView = (UIImageView *)[self.window viewWithTag:101];

    if (b.tag == 102) {

        curPage--;

        if (curPage<1) {

            curPage = 8;

        }

    }else{

        curPage++;

        if (curPage>8) {

            curPage = 1;

        }

    }

    // 获取图片名的字符串

    NSString *str =[NSString stringWithFormat:@"%d.jpg",curPage];

    imgView.image = [UIImage imageNamed:str];

    label.text = str;

    // str图片名 根据图片名获取图片

    imgView.image = [UIImage imageNamed:str];


}

BOOL tap = NO;

- (void)myTap:(UITapGestureRecognizer *)obj

{

    // 手势可以用作的UIView传递下来

    NSLog(@"%@",obj.view);

    // 可以将当前手势作用的位置传递下来

    // 参数是哪个视图 就会转化为该视图的坐标

    CGPoint point = [obj locationInView:obj.view];

    

    //点击一下就放大

    UIImageView * imgView = (UIImageView *)obj.view;

    

    //添加动画

    //开始动画

    [UIView  beginAnimations:nil context:nil];

    //设置动画持续时间

    [UIView setAnimationDuration:1.0];

    if (tap) {

        imgView.frame = CGRectMake(0, 0, 320, 480);

        //之余顶层

        [self.window bringSubviewToFront:imgView];

    }else{

        imgView.frame = CGRectMake(0, 20, 250, 300);

        imgView.center = CGPointMake(160, 240);

    }

    tap = !tap ;

    //提交动画

    [UIView commitAnimations];

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值