UIButton 同时添加图片和文字 防止图片变形

5 篇文章 0 订阅
4 篇文章 0 订阅

    [_gooleImageView setContentScaleFactor:[[UIScreen mainScreen] scale]];

    _gooleImageView.autoresizingMask = UIViewAutoresizingFlexibleHeight;

    _gooleImageView.contentMode = UIViewContentModeScaleAspectFill;

    _gooleImageView.clipsToBounds = YES;




for (int i =0; i<3; i++)

    {

       for (int j =0; j<10; j++)

        {

           if (i==2&&j>=2)

            {

               break;

            }

            

            UIButton *btn = [UIButtonbuttonWithType:UIButtonTypeCustom];

            btn.backgroundColor = [UIColorclearColor];

            [btn setBackgroundImage:[UIImageimageNamed:@"BetGrayBall"]forState:UIControlStateNormal];

            [btn setTitleColor:[UIColorblackColor] forState:UIControlStateNormal];

            btn.frame =CGRectMake(j*30,40+i*30, 30, 30);

            [btn addTarget:selfaction:@selector(zlfushiAction:)forControlEvents:UIControlEventTouchUpInside];

            [btn setTitle:[NSStringstringWithFormat:@"%d",(3+j)+i*10]forState:UIControlStateNormal];

            btn.userInteractionEnabled =YES;

            btn.tag1000+i*10+j;

            [zlhezhiVaddSubview:btn];

           NSLog(@"我的计算 %D * %D",i*10,j);

        }

    }



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

       for (int j =0; j<5; j++) {

           if (i == 2&&j ==5) {

               break;

            }

        UIButton *btn =  [UIButtonbuttonWithType:UIButtonTypeCustom];

        btn.backgroundColor = [UIColorclearColor];

        [btn setBackgroundImage:[UIImageimageNamed:@"BetGrayBall"]forState:UIControlStateNormal];

        btn.tag =1000+i*5+j;

        btn.frame =CGRectMake(55+j*46,90+i*41,35, 39);//55+(35+11)*j, 9+(39+2)*i, 35, 39

        [btn addTarget:selfaction:@selector(sanGrounDoAction:)forControlEvents:UIControlEventTouchUpInside];

        btn.userInteractionEnabled =YES;

        [self.viewaddSubview:btn];

       NSLog(@"bt.san.width %f",btn.frame.origin.x);

            

        UILabel *upOneLabel = [[UILabelalloc]init ];//]WithFrame:CGRectMake(i, 3, 20, 20)];

        upOneLabel.frame = btn.bounds;

        upOneLabel.text = [NSStringstringWithFormat:@"%d",i*5+j];

        upOneLabel.backgroundColor = [UIColorclearColor];

        upOneLabel.textColor = [UIColorcolorWithRed:160.0/255.0green:14.0/255.0blue:54.0/255.0alpha:1];

        upOneLabel.font = [UIFontsystemFontOfSize:18];

        [upOneLabel setTextAlignment:NSTextAlignmentCenter];

        [btnaddSubview:upOneLabel];

        }

    }




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

           for (NSInteger j =0; j<5; j++) {

            

               _redBallImage  = [[UIImageViewalloc] initWithImage:[UIImageimageNamed:@"BetGrayBall"]];

               _redBallImage.tag = j+i*5+100;

               _redBallImage.frame =CGRectMake(55+(35+11)*j,9+(39+2)*i,35, 39);

                _redBallImage.userInteractionEnabled =YES;

                [self.contentViewaddSubview:_redBallImage];

                [_redBallImagerelease];

                

                

               UILabel *_lableNumber = [[UILabelalloc] initWithFrame:_redBallImage.bounds];

                _lableNumber.userInteractionEnabled =YES;

                _lableNumber.tag = (j+i*5)+110;

                _lableNumber.backgroundColor = [UIColorclearColor];

                _lableNumber.text = [NSStringstringWithFormat:@"%d",j+i*5];

                _lableNumber.textColor = [UIColorcolorWithRed:160.0/255.0green:14.0/255.0blue:54.0/255.0alpha:1];

                _lableNumber.textAlignment =NSTextAlignmentCenter;

                _lableNumber.font = [UIFontsystemFontOfSize:20];

                [_redBallImageaddSubview:_lableNumber];

                [_lableNumberrelease];

                

            }

        }

在外围的循环中添加 userInteractionEnabled  =  YES;实现UITouch的三个方法根据自己的需求实现不同的功能。

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

    

    //[self.delegate tableViewScrll:YES];

   UITouch *touch = [touches anyObject];

    

    //点击后取得在 cell上的坐标点

   CGPoint locationCel = [touch locationInView:self.contentView];

    //点击后取得在window上的坐标点

   CGPoint locationWin = [touch locationInView:[UIApplication sharedApplication].delegate.window];

   NSLog(@"x1:%f,y1:%f",locationCel.x,locationCel.y);

   NSLog(@"x2:%f,y2:%f",locationWin.x,locationWin.y);

}    

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event

{

   UITouch *touch = [touches anyObject];

    //点击后取得在 cell上的坐标点

   CGPoint locationCel = [touch locationInView:self.contentView];

    //点击后取得在window上的坐标点

    CGPoint locationWin = [touchlocationInView:[UIApplicationsharedApplication].delegate.window];


}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event

{

   UITouch *touch = [touches anyObject];

   CGPoint location = [touch locationInView:self.contentView];

}
仿照 UIImageView实现按钮效果来实现。
钦此!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值