点击效果,给 UIButton UIView UIScrollView 加一个灰色的点击效果

如果要给button 或者是label 增加点击效果,那么可以这么做

        btnCurrentHot.frame=CGRectMake(60+sizeWeight+offSet,2,20,26);//指定button的frame

        btnCurrentHot.titleLabel.font=[UIFontboldSystemFontOfSize:12];//指定button的字体大小

        btnCurrentHot.tag=i;

        CGSize  size=[[arrobjectAtIndex:i]sizeWithFont:btnCurrentHot.titleLabel.fontconstrainedToSize:CGSizeMake(MAXFLOAT,26)];//根据button上面的字体大小调整button的frame button的高度不变,宽度自动调整

       UIView *bgView = [[[UIViewalloc]initWithFrame:CGRectMake(0,0, btnCurrentHot.frame.size.width-8 , btnCurrentHot.frame.size.height-25)]autorelease];//设定button 点击效果的view

        bgView.backgroundColor = [UIColorcolorWithWhite:0.0alpha:0.2];//给定这个view的alpha 淡灰色

        

        [btnCurrentHotsetFrame:CGRectMake(60+sizeWeight+offSet,2,size.width,26)];//指定button的frame 这次是根据lable 调整后的宽度

        [btnCurrentHot setTitle:[arrobjectAtIndex:i]forState:UIControlStateNormal];

        [btnCurrentHot setTitleColor:[UIColorredColor]forState:UIControlStateNormal];

        [btnCurrentHot addTarget:selfaction:@selector(btnClickCurrentHot:)forControlEvents:UIControlEventTouchUpInside];

        [btnCurrentHot setBackgroundImage:[selfgetImageFromView:bgView]forState:UIControlStateHighlighted];//指定button点击的效果


然后在下面增加这个方法:

-(UIImage *)getImageFromView:(UIView *)view{

    UIGraphicsBeginImageContext(view.bounds.size);

    [view.layerrenderInContext:UIGraphicsGetCurrentContext()];

    UIImage *image =UIGraphicsGetImageFromCurrentImageContext();

    UIGraphicsEndImageContext();

   return image;

}

这个方法,我倒不是很懂了,目测就是获取上面view的大小,然后做动作什么的,有懂的大虾,请继续补充啊!菜鸟定不胜感谢!

上面这个方法只是给button 设置点击态,如果你也想我一样遇到一个BT的UI的话,好吧,下面的方法估计能帮到你

首先给自定义的View添加点击态

在自定义的view中 写入方法

-(UIView *)backgroundView

{

    if (!_backgroundView) {

        _backgroundView=[[UIView alloc] initWithFrame:self.bounds];

        [self addSubview:_backgroundView];

        _backgroundView.autoresizingMask=UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;

        _backgroundView.alpha=0.2;

        _backgroundView.hidden=YES;

    }

    _backgroundView.frame=self.bounds;

    return _backgroundView;

}

-(void)setHBackgroundColor:(UIColor *)hBackgroundColor

{

    [hBackgroundColor release];

    hBackgroundColor=[hBackgroundColor retain];

    self.backgroundView.backgroundColor=hBackgroundColor;

}


因为每个view都有它的触发方法

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

{

    [self bringSubviewToFront:self.backgroundView];

    self.backgroundView.hidden=NO;

   

}

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

{

    self.backgroundView.hidden=YES;

}

好吧,在外面初始化这个自定义的view 的时候给它一个高亮的颜色  


那这个是给view 如果你还像我那么倒楣的话,要给scrollview 添加点击态,是scrollview 啊  
Oh,My God 
 那么好吧 这个方法

-(UIView *)hBackGroundView

{

    if (!_hBackGroundView) {

        _hBackGroundView =[[UIView alloc]initWithFrame:self.bounds];

        [self addSubview:_hBackGroundView];

        _hBackGroundView.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleWidth;

        _hBackGroundView.alpha=0.2;

        _hBackGroundView.hidden=YES;

        _hBackGroundView.bounds=self.bounds;

    }

    else {

        _hBackGroundView.size=self.contentSize;

    }

    return _hBackGroundView;

}


其他都相同的
 首先如果你不这么做的话,点击态有效果的就只有scrollview的第一张了,因为你的 _hBackGroundView
是在当前的 也就是第一张上面添加的,滑动的话 就没有了,所以把_hBackGroundView 的size 设置为scroll view 的contentsize 就好了




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值