在最近的项目中,总发现给UISegment贴图时有不想要的效果出现,自己就写了一个简单的view,冒充一哈Senment,欢迎补充方法哈
CSegmentView.h
#import <UIKit/UIKit.h>
@interface CSegmentView : UIView{
id target;
SEL action;
}
@property(nonatomic,retain)NSMutableArray* itemArray;
@property(nonatomic,assign)int selectIndex;
@property(nonatomic,retain)NSArray* imageNameNormal;
@property(nonatomic,retain)NSArray* imageNameSelect;
- (id)initWithArray:(NSArray*)imageN imageNameSelected:(NSArray*)arrayS target:(id)targetT action:(SEL)actionT;
@end
CSegmentView.m
#import "CSegmentView.h"
@implementation CSegmentView
@synthesize itemArray = _itemArray;
@synthesize selectIndex = _selectIndex;
@synthesize imageNameNormal = _imageNameNormal;
@synthesize imageNameSelect = _imageNameSelect;
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
// Initialization code
}
return self;
}
- (id)initWithArray:(NSArray*)imageN imageNameSelected:(NSArray*)arrayS target:(id)targetT action:(SEL)actionT{
self = [super init];
if (self) {
target = targetT;
action = actionT;
_imageNameSelect = [arrayS retain];
_imageNameNormal = [imageN retain];
[self setBackgroundColor:[UIColor clearColor]];
_itemArray = [[NSMutableArray alloc] init];
int imageCount = [imageN count];
for (int i = 0;i<imageCount; i++) {
UIImage* image = [UIImage imageNamed:[imageN objectAtIndex:i]];
UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setImage:image forState:UIControlStateNormal];
[button setImage:image forState:UIControlStateHighlighted];
[button setFrame:CGRectMake(i*image.size.width, 0, image.size.width, image.size.height)];
[button addTarget:self action:@selector(onButtonClick:) forControlEvents:UIControlEventTouchUpInside];
button.tag = i;
[self addSubview:button];
[_itemArray insertObject:button atIndex:i];
}
}
return self;
}
- (void)dealloc {
[_itemArray removeAllObjects];
[_itemArray release];
[_imageNameNormal release];
[_imageNameSelect release];
[super dealloc];
}
-(void)onButtonClick:(UIButton*)button{
if (0<=_selectIndex && _selectIndex<[_itemArray count]) {
UIButton* buttonP = [_itemArray objectAtIndex:_selectIndex];
[buttonP setImage:[UIImage imageNamed:[_imageNameNormal objectAtIndex:buttonP.tag]] forState:UIControlStateHighlighted];
[buttonP setImage:[UIImage imageNamed:[_imageNameNormal objectAtIndex:buttonP.tag]] forState:UIControlStateNormal];
}
_selectIndex = button.tag;
[button setImage:[UIImage imageNamed:[_imageNameSelect objectAtIndex:_selectIndex]] forState:UIControlStateNormal];
[button setImage:[UIImage imageNamed:[_imageNameSelect objectAtIndex:_selectIndex]] forState:UIControlStateHighlighted];
[target performSelector:action withObject:self];
}
-(void)setSelectIndex:(int)selectIndex{
if (selectIndex>=0 && selectIndex<[_itemArray count]) {
[self onButtonClick:[_itemArray objectAtIndex:selectIndex]];
}else{
if (_selectIndex>=0 && _selectIndex<[_itemArray count]){
UIButton* buttonP = [_itemArray objectAtIndex:_selectIndex];
[buttonP setImage:[UIImage imageNamed:[_imageNameNormal objectAtIndex:_selectIndex]] forState:UIControlStateHighlighted];
[buttonP setImage:[UIImage imageNamed:[_imageNameNormal objectAtIndex:_selectIndex]] forState:UIControlStateNormal];
}
_selectIndex = selectIndex;
}
}
@end
使用:
-(CSegmentView*)_segmentedBar{
if (nil == _segmentedBar) {
_segmentedBar=[[CSegmentView alloc] initWithArray:[NSArray arrayWithObjects:@"button-recent1.png",@"button-popular1.png",@"button-random1.png", nil] imageNameSelected:[NSArray arrayWithObjects:@"button-recent2.png",@"button-popular2.png",@"button-random2.png", nil] target:self action:@selector(tabBarButtonDown:)];
_segmentedBar.frame=UIInterfaceOrientationIsPortrait(self.interfaceOrientation)?CGRectMake(249, 7, 270, 30):CGRectMake(377, 7, 270, 30);
_segmentedBar.selectIndex = -1;
}
return _segmentedBar;
}