網站首頁 編程語言 正文
本文實例為大家分享了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
相關推薦
- 2022-08-29 Python通用驗證碼識別OCR庫ddddocr的安裝使用教程_python
- 2023-02-25 一文搞懂Python中is和==的區別_python
- 2022-06-25 如何利用Pandas刪除某列指定值所在的行_python
- 2022-12-23 Mariadb數據庫主從復制同步配置過程實例_mariadb
- 2022-08-18 nginx之內存池的實現_nginx
- 2022-07-12 mongoDB替換replace某個字段的部分內容
- 2023-01-30 PyQt中使用QProcess運行一個進程的示例代碼_python
- 2023-04-24 一文帶你深入了解C++中音頻PCM數據_C 語言
- 最近更新
-
- 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同步修改后的遠程分支