如果要给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;
}
-(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;
}
-(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;
}