網站首頁 編程語言 正文
本文實例為大家分享了iOS自定義滑桿的具體代碼,供大家參考,具體內容如下
先讓我們看看效果:
主要實現的代碼:
UIImage *thumbWithLevel(float aLevel)
{
? ? float INSET_AMT = 1.5f;
? ? CGRect baseRect = CGRectMake(0, 0, 40, 100);
? ? CGRect thumbRect = CGRectMake(0, 40, 40, 20);
? ??
? ? UIGraphicsBeginImageContext(baseRect.size);
? ? CGContextRef context = UIGraphicsGetCurrentContext();
? ??
? ? [[UIColor darkGrayColor] setFill];
? ? CGContextAddRect(context, CGRectInset(thumbRect, INSET_AMT, INSET_AMT));
? ? CGContextFillPath(context);
? ??
? ? [[UIColor whiteColor] setStroke];
? ? CGContextSetLineWidth(context, 2);
? ? CGContextAddRect(context, CGRectInset(thumbRect, 2 * INSET_AMT, 2 * INSET_AMT));
? ? CGRect ellipseRect = CGRectMake(0, 0, 40, 40);
? ? [[UIColor colorWithWhite:aLevel alpha:1] setFill];
? ? CGContextAddEllipseInRect(context, ellipseRect);
? ? CGContextFillPath(context);
? ??
? ? NSString *numString = [NSString stringWithFormat:@"%0.2f",aLevel];
? ? UIColor *textColor = (aLevel > 0.5) ? [UIColor blackColor] : [UIColor whiteColor];
? ? UIFont *font = [UIFont fontWithName:@"Georgia" size:15];
? ? NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
? ? style.lineBreakMode = NSLineBreakByCharWrapping;
? ? style.alignment = NSTextAlignmentCenter;
? ? NSDictionary *attr = @{NSFontAttributeName:font,NSParagraphStyleAttributeName:style,NSForegroundColorAttributeName:textColor};
? ? [numString drawInRect:CGRectInset(ellipseRect, 0, 6) withAttributes:attr];
? ??
? ? [[UIColor grayColor] setStroke];
? ? CGContextSetLineWidth(context, 3);
? ? CGContextAddEllipseInRect(context, CGRectInset(ellipseRect, 2, 2));
? ? CGContextStrokePath(context);
? ??
? ? UIImage *theImage = UIGraphicsGetImageFromCurrentImageContext();
? ? UIGraphicsEndImageContext();
? ??
? ? return ?theImage;
}
在這里我們通過context的方法將圖片畫出了,對于性能有點要求,但是現在應該不在乎這點性能了
- (void)updateThumb
{
? ? if ((self.value < 0.98) && (ABS(self.value - previousValue) < 0.1f)) {
? ? ? ? return;
? ? }
? ??
? ? UIImage *customImg = thumbWithLevel(self.value);
? ? [self setThumbImage:customImg forState:UIControlStateHighlighted];
? ? previousValue = self.value;
}
通過滑塊的值來使上面的值進行變化,更加的直觀
[self setThumbImage:simpleThumb() forState:UIControlStateNormal];
? [self addTarget:self action:@selector(startDrag:) forControlEvents:UIControlEventTouchDown];
? [self addTarget:self action:@selector(updateThumb) forControlEvents:UIControlEventValueChanged];
? [self addTarget:self action:@selector(endDrag:) forControlEvents:UIControlEventTouchUpOutside | UIControlEventTouchUpInside];
對于不同的狀態來進行不同的操作,讓滑桿的用戶體驗度更加的完整
原文鏈接:https://blog.csdn.net/ChasingDreamsCoder/article/details/53074677
相關推薦
- 2023-01-05 C++模板?index_sequence使用示例詳解_C 語言
- 2022-11-23 jQuery?validate(submitHandler函數)驗證通過發送Ajax(實例詳解)_j
- 2022-11-07 CLion開發stm32?使用DSP庫的操作方法_C 語言
- 2023-01-10 C#實現無損壓縮圖片的示例詳解_C#教程
- 2021-12-09 golang中gin框架接入jwt使用token驗證身份_Golang
- 2022-11-06 React如何接收excel文件下載導出功能封裝_React
- 2023-10-13 獲取element-ui的Collapse折疊后高度
- 2022-05-13 Android 10 讀寫文件權限
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支