- 浏览: 2476151 次
- 性别:
- 来自: 苏州
文章分类
最新评论
-
jsntghf:
peio 写道这个怎么运行?Ruby On Rails的环境搭 ...
多文件上传之uploadify -
peio:
这个怎么运行?
多文件上传之uploadify -
往事如烟1:
我的项目是自己init了一个原始的project,之后将ver ...
React Native热部署之CodePush -
jsntghf:
往事如烟1 写道我按照你的说明进行,发现app退出之后,在进入 ...
React Native热部署之CodePush -
往事如烟1:
我按照你的说明进行,发现app退出之后,在进入不正确,请问是什 ...
React Native热部署之CodePush
CustomSegmentedControl.h
@class CustomSegmentedControl; @protocol CustomSegmentedControlDelegate - (UIButton *) buttonFor:(CustomSegmentedControl *)segmentedControl atIndex:(NSUInteger)segmentIndex; @optional - (void) touchUpInsideSegmentIndex:(NSUInteger)segmentIndex; - (void) touchDownAtSegmentIndex:(NSUInteger)segmentIndex; @end @interface CustomSegmentedControl : UIView { NSObject <CustomSegmentedControlDelegate> *delegate; NSMutableArray *buttons; } @property (nonatomic, retain) NSMutableArray *buttons; - (id) initWithSegmentCount:(NSUInteger)segmentCount segmentsize:(CGSize)segmentsize dividerImage:(UIImage *)dividerImage tag:(NSInteger)objectTag delegate:(NSObject <CustomSegmentedControlDelegate> *)customSegmentedControlDelegate; @end
CustomSegmentedControl.m
#import "CustomSegmentedControl.h" @implementation CustomSegmentedControl @synthesize buttons; - (id) initWithSegmentCount:(NSUInteger)segmentCount segmentsize:(CGSize)segmentsize dividerImage:(UIImage *)dividerImage tag:(NSInteger)objectTag delegate:(NSObject <CustomSegmentedControlDelegate> *)customSegmentedControlDelegate { if (self = [super init]) { self.tag = objectTag; delegate = customSegmentedControlDelegate; self.frame = CGRectMake(0, 0, (segmentsize.width * segmentCount) + (dividerImage.size.width * (segmentCount - 1)), segmentsize.height); self.buttons = [[NSMutableArray alloc] initWithCapacity:segmentCount]; CGFloat horizontalOffset = 0; for (NSUInteger i = 0; i < segmentCount; i++) { UIButton *button = [delegate buttonFor:self atIndex:i]; [button addTarget:self action:@selector(touchDownAction:) forControlEvents:UIControlEventTouchDown]; [button addTarget:self action:@selector(touchUpInsideAction:) forControlEvents:UIControlEventTouchUpInside]; [button addTarget:self action:@selector(otherTouchesAction:) forControlEvents:UIControlEventTouchUpOutside]; [button addTarget:self action:@selector(otherTouchesAction:) forControlEvents:UIControlEventTouchDragOutside]; [button addTarget:self action:@selector(otherTouchesAction:) forControlEvents:UIControlEventTouchDragInside]; [buttons addObject:button]; button.frame = CGRectMake(horizontalOffset, 0.0, button.frame.size.width, button.frame.size.height); [self addSubview:button]; if (i != segmentCount - 1) { UIImageView *divider = [[[UIImageView alloc] initWithImage:dividerImage] autorelease]; divider.frame = CGRectMake(horizontalOffset + segmentsize.width, 0.0, dividerImage.size.width, dividerImage.size.height); [self addSubview:divider]; } horizontalOffset = horizontalOffset + segmentsize.width + dividerImage.size.width; } } return self; } - (void) dimAllButtonsExcept:(UIButton *)selectedButton { for (UIButton *button in buttons) { if (button == selectedButton) { button.selected = YES; button.highlighted = button.selected ? NO : YES; } else { button.selected = NO; button.highlighted = NO; } } } - (void)touchDownAction:(UIButton *)button { [self dimAllButtonsExcept:button]; if ([delegate respondsToSelector:@selector(touchDownAtSegmentIndex:)]) [delegate touchDownAtSegmentIndex:[buttons indexOfObject:button]]; } - (void)touchUpInsideAction:(UIButton *)button { [self dimAllButtonsExcept:button]; if ([delegate respondsToSelector:@selector(touchUpInsideSegmentIndex:)]) [delegate touchUpInsideSegmentIndex:[buttons indexOfObject:button]]; } - (void)otherTouchesAction:(UIButton *)button { [self dimAllButtonsExcept:button]; } - (void)dealloc { [buttons release]; [super dealloc]; } @end
CustomSegmentedControlsViewController.h
#import "CustomSegmentedControl.h" @interface CustomSegmentedControlsViewController : UIViewController <CustomSegmentedControlDelegate> { } @end
CustomSegmentedControlsViewController.m
#import "CustomSegmentedControlsViewController.h" #define VERTICAL_OFFSET 10.0 #define HORIZONTAL_OFFSET 10.0 #define VERTICAL_SPACING 5.0 #define VERTICAL_HEIGHT 70.0 #define TAG_VALUE 9000 static NSArray *buttons = nil; typedef enum { CapLeft = 0, CapMiddle = 1, CapRight = 2, CapLeftAndRight = 3 } CapLocation; @interface CustomSegmentedControlsViewController (PrivateMethods) - (void) addView:(UIView *)subView verticalOffset:(NSUInteger)verticalOffset title:(NSString *)title; @end @implementation CustomSegmentedControlsViewController - (void) awakeFromNib { buttons = [[NSArray arrayWithObjects: [NSDictionary dictionaryWithObjectsAndKeys:[NSArray arrayWithObjects:@"One", @"Two", @"Three", nil], @"titles", [NSValue valueWithCGSize:CGSizeMake(80, 47)], @"size", @"bottombarblue.png", @"button-image", @"bottombarblue_pressed.png", @"button-highlight-image", @"blue-divider.png", @"divider-image", [NSNumber numberWithFloat:14.0], @"cap-width", nil], [NSDictionary dictionaryWithObjectsAndKeys:[NSArray arrayWithObjects:@"One", @"Two", nil], @"titles", [NSValue valueWithCGSize:CGSizeMake(80, 47)], @"size", @"bottombarblue.png", @"button-image", @"bottombarblue_pressed.png", @"button-highlight-image", @"blue-divider.png", @"divider-image", [NSNumber numberWithFloat:14.0], @"cap-width", nil], [NSDictionary dictionaryWithObjectsAndKeys:[NSArray arrayWithObjects:@"One", @"Two", @"Three", nil], @"titles", [NSValue valueWithCGSize:CGSizeMake(100, 47)], @"size", @"bottombarredfire.png", @"button-image", @"bottombarredfire_pressed.png", @"button-highlight-image", @"red-divider.png", @"divider-image", [NSNumber numberWithFloat:14.0], @"cap-width", nil], [NSDictionary dictionaryWithObjectsAndKeys:[NSArray arrayWithObjects:@"1", @"2", nil], @"titles", [NSValue valueWithCGSize:CGSizeMake(47, 47)], @"size", @"bottombarredfire.png", @"button-image", @"bottombarredfire_pressed.png", @"button-highlight-image", @"red-divider.png", @"divider-image", [NSNumber numberWithFloat:14.0], @"cap-width", nil], nil] retain]; } - (void) viewDidLoad { [super viewDidLoad]; NSDictionary *blueSegmentedControlData = [buttons objectAtIndex:0]; NSArray *blueSegmentedControlTitles = [blueSegmentedControlData objectForKey:@"titles"]; CustomSegmentedControl *blueSegmentedControl = [[[CustomSegmentedControl alloc] initWithSegmentCount:blueSegmentedControlTitles.count segmentsize:[[blueSegmentedControlData objectForKey:@"size"] CGSizeValue] dividerImage:[UIImage imageNamed:[blueSegmentedControlData objectForKey:@"divider-image"]] tag:TAG_VALUE delegate:self] autorelease]; [self addView:blueSegmentedControl verticalOffset:0 title:@"Blue segmented control"]; NSDictionary *anotherBlueSegmentedControlData = [buttons objectAtIndex:1]; NSArray *anotherBlueSegmentedControlTitles = [anotherBlueSegmentedControlData objectForKey:@"titles"]; CustomSegmentedControl *anotherBlueSegmentedControl = [[[CustomSegmentedControl alloc] initWithSegmentCount:anotherBlueSegmentedControlTitles.count segmentsize:[[anotherBlueSegmentedControlData objectForKey:@"size"] CGSizeValue] dividerImage:[UIImage imageNamed:[anotherBlueSegmentedControlData objectForKey:@"divider-image"]] tag:TAG_VALUE + 1 delegate:self] autorelease]; [self addView:anotherBlueSegmentedControl verticalOffset:1 title:@"Another blue segmented control"]; NSDictionary *redSegmentedControlData = [buttons objectAtIndex:2]; NSArray *redSegmentedControlTitles = [redSegmentedControlData objectForKey:@"titles"]; CustomSegmentedControl *redSegmentedControl = [[[CustomSegmentedControl alloc] initWithSegmentCount:redSegmentedControlTitles.count segmentsize:[[redSegmentedControlData objectForKey:@"size"] CGSizeValue] dividerImage:[UIImage imageNamed:[redSegmentedControlData objectForKey:@"divider-image"]] tag:TAG_VALUE + 2 delegate:self] autorelease]; [self addView:redSegmentedControl verticalOffset:2 title:@"Red segmented control"]; NSDictionary *anotherRedSegmentedControlData = [buttons objectAtIndex:3]; NSArray *anotherRedSegmentedControlTitles = [anotherRedSegmentedControlData objectForKey:@"titles"]; CustomSegmentedControl *anotherRedSegmentedControl = [[[CustomSegmentedControl alloc] initWithSegmentCount:anotherRedSegmentedControlTitles.count segmentsize:[[anotherRedSegmentedControlData objectForKey:@"size"] CGSizeValue] dividerImage:[UIImage imageNamed:[anotherRedSegmentedControlData objectForKey:@"divider-image"]] tag:TAG_VALUE + 3 delegate:self] autorelease]; [self addView:anotherRedSegmentedControl verticalOffset:3 title:@"Another red segmented control"]; } - (void) addView:(UIView *)subView verticalOffset:(NSUInteger)verticalOffset title:(NSString *)title { CGFloat elementVerticalLocation = (VERTICAL_HEIGHT + (VERTICAL_SPACING * 2)) * verticalOffset; UILabel *label = [[[UILabel alloc] initWithFrame:CGRectMake(HORIZONTAL_OFFSET, elementVerticalLocation + VERTICAL_OFFSET, 0, 0)] autorelease]; label.backgroundColor = [UIColor clearColor]; label.textColor = [UIColor whiteColor]; label.text = title; [label sizeToFit]; [self.view addSubview:label]; subView.frame = CGRectMake(HORIZONTAL_OFFSET, elementVerticalLocation + label.frame.size.height + 5 + VERTICAL_OFFSET, subView.frame.size.width, subView.frame.size.height); [self.view addSubview:subView]; } - (UIImage *) image:(UIImage *)image withCap:(CapLocation)location capWidth:(NSUInteger)capWidth buttonWidth:(NSUInteger)buttonWidth { UIGraphicsBeginImageContextWithOptions(CGSizeMake(buttonWidth, image.size.height), NO, 0.0); if (location == CapLeft) [image drawInRect:CGRectMake(0, 0, buttonWidth + capWidth, image.size.height)]; else if (location == CapRight) [image drawInRect:CGRectMake(0.0 - capWidth, 0, buttonWidth + capWidth, image.size.height)]; else if (location == CapMiddle) [image drawInRect:CGRectMake(0.0 - capWidth, 0, buttonWidth + (capWidth * 2), image.size.height)]; UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext(); UIGraphicsEndImageContext(); return resultImage; } #pragma mark - #pragma mark CustomSegmentedControlDelegate - (UIButton *) buttonFor:(CustomSegmentedControl *)segmentedControl atIndex:(NSUInteger)segmentIndex { NSUInteger dataOffset = segmentedControl.tag - TAG_VALUE ; NSDictionary *data = [buttons objectAtIndex:dataOffset]; NSArray *titles = [data objectForKey:@"titles"]; CapLocation location; if (segmentIndex == 0) location = CapLeft; else if (segmentIndex == titles.count - 1) location = CapRight; else location = CapMiddle; UIImage *buttonImage = nil; UIImage *buttonPressedImage = nil; CGFloat capWidth = [[data objectForKey:@"cap-width"] floatValue]; CGSize buttonSize = [[data objectForKey:@"size"] CGSizeValue]; if (location == CapLeftAndRight) { buttonImage = [[UIImage imageNamed:[data objectForKey:@"button-image"]] stretchableImageWithLeftCapWidth:capWidth topCapHeight:0.0]; buttonPressedImage = [[UIImage imageNamed:[data objectForKey:@"button-highlight-image"]] stretchableImageWithLeftCapWidth:capWidth topCapHeight:0.0]; } else { buttonImage = [self image:[[UIImage imageNamed:[data objectForKey:@"button-image"]] stretchableImageWithLeftCapWidth:capWidth topCapHeight:0.0] withCap:location capWidth:capWidth buttonWidth:buttonSize.width]; buttonPressedImage = [self image:[[UIImage imageNamed:[data objectForKey:@"button-highlight-image"]] stretchableImageWithLeftCapWidth:capWidth topCapHeight:0.0] withCap:location capWidth:capWidth buttonWidth:buttonSize.width]; } UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; button.frame = CGRectMake(0.0, 0.0, buttonSize.width, buttonSize.height); [button setTitle:[titles objectAtIndex:segmentIndex] forState:UIControlStateNormal]; [button setBackgroundImage:buttonImage forState:UIControlStateNormal]; [button setBackgroundImage:buttonPressedImage forState:UIControlStateHighlighted]; [button setBackgroundImage:buttonPressedImage forState:UIControlStateSelected]; button.adjustsImageWhenHighlighted = NO; if (segmentIndex == 0) button.selected = YES; return button; } @end
效果图:
- CustomSegmentedControls.zip (7.5 KB)
- 下载次数: 77
发表评论
-
Error watching file for changes: EMFILE
2016-12-15 11:57 1237执行npm start后报错: Error watc ... -
CocoaPods升级1.1.1报错
2016-12-15 08:39 741ERROR: While executing gem .. ... -
Visual Studio Code运行React Native报错
2016-06-13 09:43 1558React Native:0.27.2 React:15 ... -
React Native 0.27.2编译报错this._nativeModule.addListener is not a function
2016-06-12 15:21 3789React Native:0.27.2 React:15 ... -
Unable to resolve module ReactDefaultPerf from
2016-06-02 13:04 2733package.json信息如下: "reac ... -
React Native 0.26.2编译报错Undefined symbols for architecture x86_64
2016-05-26 11:15 1947React Native:0.26.2 React:15. ... -
Failed to update auto layout status: Failed to load designables from path (null)
2016-04-05 22:11 1667确保CocoaPods是0.36.1以上版本,然后在podf ... -
集成微信支付出现Undefined symbols for architecture x86_64错误
2016-03-21 13:22 1683Undefined symbols for architec ... -
React Native热部署之CodePush
2016-01-10 22:27 6126本文使用的环境是Mac OS 10.11.1、Xcode ... -
浅谈React Native中的FlexBox布局
2015-11-17 18:38 4270React Native通过一个基于FlexBox的布局引 ... -
React Native之构建一个简单的列表页
2015-10-23 14:45 2096本文中我们将创建一个简单的电影应用,这个应用将从Rotten ... -
React Native之环境搭建
2015-10-20 16:30 1394本文使用的环境是Mac O ... -
获取图片属性的方法
2015-10-18 20:43 3080很多时候我们需要获 ... -
NSCache的下标用法
2015-09-18 00:19 1168NSCache类和NSDictionary类很相似,也提供 ... -
如何给category添加属性
2015-08-16 10:41 644主要是使用了runtime中的associative机制。 ... -
UITableView的两种重用Cell方法的区别
2015-08-10 13:07 16065UITableView中有两种重用Cell的方法: - ... -
SDImageCache.m报错Unused variable 'fileName'
2015-08-04 21:56 1132GCC手册中的相关解释: unused:This att ... -
Swift调用Objective-C
2015-07-13 23:33 1176Swift调用Objective-C需要一个名为<工程 ... -
使用GCD实现倒计时
2015-07-24 21:47 1045__block int timeout = 60; // ... -
导航栏加分割线的实现
2015-07-01 22:00 1692self.view.backgroundColor = [U ...
相关推荐
自定义UISegmentedControl,源代码地址:https://github.com/STShenZhaoliang/STKitSwift
采用Swift编写自定义UISegmentedControl实现的滑块Tab效果
自定义UISegmentedControl空间
作者STShenZhaoliang,源码STKitSwift,自定义UISegmentedControl
ios 自定义UISegmentedControl 横向和竖向,文字自适应或换行
利用 UISegmentedControl 实现的滑块儿 Tab 效果.zip,iOS的自定义uisegmentedcontrol替换,用swift编写
ios应用源码之自定义颜色的uisegmentedcontrol .
IOS应用源码——自定义颜色的UISegmentedControl.zip
自定义颜色的UISegmentedControl.zipIOS应用例子源码下载自定义颜色的UISegmentedControl.zipIOS应用例子源码下载 1.适合学生学习研究参考 2.适合个人学习研究参考 3.适合公司开发项目技术参考
IOS应用源码之自定义颜色的UISegmentedControl .rar
IOS应用源码之自定义颜色的UISegmentedControl.zip
IOS应用源码——自定义颜色的UISegmentedControl.rar
前两年IOS应用源码-主要是用于毕业设计学习的。
该源码实现支持自定义分段控件的效果,可以随便自定义UISegmentedControl的文字和颜色,以及图片等,另外还可以给Segment加上图片等,值得大家的学习,。
这是一款自定义分段选择特效,该特效可以自定义UISegmentedControl的文字,背景颜色,而且还可以自定义分段选择的图片,同时也支持ARC等,喜欢的朋友可以参考一下吧,。
来源:Licence:Unspecified平台:iOS设备:iPhone / iPad作者:Keiran Paste ... 自定义UISegmentedControl的文字、背景颜色和图片。支持ARC。 Code4App编译测试,适用环境:Xcode 4.3, iOS 5.0。
来源:Licence:MIT平台:iOS设备:iPhone / iPad作者:Cedric Vandendriessche ... 用户可以自定义UISegmentedControl的文字、颜色和图片。给Segment加上图片。 Code4App编译测试,适用环境:Xcode 4.3, iOS 5.0。
BetterSegmentedControl是使用Swift编写的UISegmentedControl和UISwitch的易于使用,可自定义的替代品。 产品特点 可用作分段控件或开关 可自定义选项的Plethora,从颜色到插图,半径和动画 使用文本或图标作为...
WLSegmentedControls WLHorizontalSegmentedControl是具有多选支持的UISegmentedControl的自定义实现。 WLVerticalSegmentedControl是相应的垂直版本。