- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我想知道如何制作一个看起来像KAYAK应用程序(它是一个搜索航类和酒店的旅行应用程序)中的进度 View ,截图:
我在越狱的 iPhone 上挖掘了 KAYAK 应用程序的资源,发现以下 3 张图像构成了这个进度 View :
进度条背景@2x.png
progressbar-gradient@2x.png
progressbar-overlay@2x.png
考虑到进度 View 有一个移动的叠加图像,它与渐变图像一起重复移动。
任何想法或示例代码将不胜感激。
最佳答案
我制作了整个工作包,将模仿您发布的这个进度 View 。但是,为了让它更可定制,我没有使用任何图像,而是使用 CoreGraphics 来绘制它。该软件包可以在 lightdesign/LDProgressView 找到.我也可能会把它变成 CocoaPod如果你知道那是什么。
如何绘制类似 KAYAK 的进度 View
进度 View 的所有内部工作原理以及如何模仿 KAYAK 进度 View 可以在 this file 中找到。 .为了更容易理解,我在代码块中添加了一些注释。这是drawRect
方法:
- (void)drawRect:(CGRect)rect {
[self setAnimateIfNotSet];
CGContextRef context = UIGraphicsGetCurrentContext();
[self drawProgressBackground:context inRect:rect];
if (self.progress > 0) {
[self drawProgress:context withFrame:rect];
}
}
drawProgressBackground:inRect:
方法:
- (void)drawProgressBackground:(CGContextRef)context inRect:(CGRect)rect {
CGContextSaveGState(context);
// Draw the background with a gray color within a rounded rectangle
UIBezierPath *roundedRect = [UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:10];
CGContextSetFillColorWithColor(context, [UIColor colorWithRed:0.51f green:0.51f blue:0.51f alpha:1.00f].CGColor);
[roundedRect fill];
// Create the inner shadow path
UIBezierPath *roundedRectangleNegativePath = [UIBezierPath bezierPathWithRect:CGRectMake(-10, -10, rect.size.width+10, rect.size.height+10)];
[roundedRectangleNegativePath appendPath:roundedRect];
roundedRectangleNegativePath.usesEvenOddFillRule = YES;
CGSize shadowOffset = CGSizeMake(0.5, 1);
CGContextSaveGState(context);
CGFloat xOffset = shadowOffset.width + round(rect.size.width);
CGFloat yOffset = shadowOffset.height;
CGContextSetShadowWithColor(context,
CGSizeMake(xOffset + copysign(0.1, xOffset), yOffset + copysign(0.1, yOffset)), 5, [[UIColor blackColor] colorWithAlphaComponent:0.7].CGColor);
// Draw the inner shadow
[roundedRect addClip];
CGAffineTransform transform = CGAffineTransformMakeTranslation(-round(rect.size.width), 0);
[roundedRectangleNegativePath applyTransform:transform];
[[UIColor grayColor] setFill];
[roundedRectangleNegativePath fill];
CGContextRestoreGState(context);
}
10
的圆角矩形。 (稍后我可能允许对其进行自定义)并填充它。然后剩下的代码是绘制内部阴影,我真的不需要详细介绍。现在,这是在方法
drawProgress:withFrame:
中绘制进度的代码:
- (void)drawProgress:(CGContextRef)context withFrame:(CGRect)frame {
CGRect rectToDrawIn = CGRectMake(0, 0, frame.size.width * self.progress, frame.size.height);
CGRect insetRect = CGRectInset(rectToDrawIn, 0.5, 0.5);
UIBezierPath *roundedRect = [UIBezierPath bezierPathWithRoundedRect:insetRect cornerRadius:10];
if ([self.flat boolValue]) {
CGContextSetFillColorWithColor(context, self.color.CGColor);
[roundedRect fill];
} else {
CGContextSaveGState(context);
[roundedRect addClip];
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat locations[] = {0.0, 1.0};
NSArray *colors = @[(__bridge id)[self.color lighterColor].CGColor, (__bridge id)[self.color darkerColor].CGColor];
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colors, locations);
CGContextDrawLinearGradient(context, gradient, CGPointMake(insetRect.size.width / 2, 0), CGPointMake(insetRect.size.width / 2, insetRect.size.height), 0);
CGContextRestoreGState(context);
CGGradientRelease(gradient);
CGColorSpaceRelease(colorSpace);
}
CGContextSetStrokeColorWithColor(context, [[self.color darkerColor] darkerColor].CGColor);
[self drawStripes:context inRect:insetRect];
[roundedRect stroke];
[self drawRightAlignedLabelInRect:insetRect];
}
self.progress
计算进度占用的帧数。属性(property)。其次,如果
flat
,我绘制纯色属性已设置,或者我绘制了一个计算梯度(方法
lighterColor
和
darkerColor
属于
UIColor
类别)。第三,我画条纹,最后画百分比标签。让我们快速介绍这两种方法。这是
drawStripes:inRect:
方法:
- (void)drawStripes:(CGContextRef)context inRect:(CGRect)rect {
CGContextSaveGState(context);
[[UIBezierPath bezierPathWithRoundedRect:rect cornerRadius:10] addClip];
CGContextSetFillColorWithColor(context, [[UIColor whiteColor] colorWithAlphaComponent:0.2].CGColor);
CGFloat xStart = self.offset, height = rect.size.height, width = STRIPE_WIDTH;
while (xStart < rect.size.width) {
CGContextSaveGState(context);
CGContextMoveToPoint(context, xStart, height);
CGContextAddLineToPoint(context, xStart + width * 0.25, 0);
CGContextAddLineToPoint(context, xStart + width * 0.75, 0);
CGContextAddLineToPoint(context, xStart + width * 0.50, height);
CGContextClosePath(context);
CGContextFillPath(context);
CGContextRestoreGState(context);
xStart += width;
}
CGContextRestoreGState(context);
}
self.offset
绘制这些条纹介于
-STRIPE_WIDTH
之间和
0
由计时器递增。然后,我创建了一个简单的循环,以便只创建足够的条纹来完全填充 View 的进度部分。我也留下了
STRIPE_WIDTH
的25%空白,这样条纹就不会相互挤在一起。这是最终的绘制方法
drawRightAlignedLabelInRect:
:
- (void)drawRightAlignedLabelInRect:(CGRect)rect {
UILabel *label = [[UILabel alloc] initWithFrame:rect];
label.backgroundColor = [UIColor clearColor];
label.textAlignment = NSTextAlignmentRight;
label.text = [NSString stringWithFormat:@"%.0f%%", self.progress*100];
label.font = [UIFont boldSystemFontOfSize:17];
UIColor *baseLabelColor = [self.color isLighterColor] ? [UIColor blackColor] : [UIColor whiteColor];
label.textColor = [baseLabelColor colorWithAlphaComponent:0.6];
[label drawTextInRect:CGRectOffset(rect, -6, 0)];
}
0.0
和
1.0
之间)转换为百分比(从
0%
到
100%
)。然后我根据所选进度颜色的深浅将颜色设置为深或浅,并在
CGContext
中绘制标签。 .
LDProgressView
的实例上设置三个属性。或事先在
UIAppearance
方法。
UIAppearance
方法将是这样的:
[[LDProgressView appearance] setColor:[UIColor colorWithRed:0.87f green:0.55f blue:0.09f alpha:1.00f]];
color
属性(property)。此
UIAppearance
方法看起来像这样:
[[LDProgressView appearance] setFlat:@NO];
UIAppearance
也可以为
LDProgressView
的所有实例一般设置方法看起来像这样:
[[LDProgressView appearance] setAnimate:@YES];
关于iphone - 如何制作类似于客涯应用中的进度 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18887142/
是否有一种 STL 算法允许我将一个函数应用于一个范围内的每个元素,转换元素,并将之前转换的元素作为输入? 我在想这样的事情(显然行不通,因为第二个迭代器将无效): struct Input {
我有一个字典列表,例如: l =[{country:'Italy',sales:100,cost:50}{country:'Italy',sales:130,cost:60} {co
考虑以下几点: $var = 'Now is the time' if ($var -like 'Now*') { 'true' } else { 'false' } 输出:真 现在交换 -like
我认为这是一个简单的问题,但尚未得到解决方案。我只想从此处解释的列中获取有效数字。 假设我们有一个包含以下值的 varchar 列 ABC Italy Apple 234.62 2:234:43:22
这个问题已经有答案了: MySQL LIKE IN()? (12 个回答) 已关闭 4 年前。 是否可以使用 IN 子句扩展 LIKE 表达式? 此时我得到以下 SQL: select * from
这个问题在这里已经有了答案: How to postpone/defer the evaluation of f-strings? (14 个答案) 关闭 3 年前。 考虑字符串 string_0
关闭。这个问题需要details or clarity .它目前不接受答案。 想改进这个问题吗? 通过 editing this post 添加细节并澄清问题. 关闭 8 年前。 Improve t
我刚刚阅读了以下关于同一主题的帖子: Facebook like notifications tracking (DB Design)和 Database design to store notifi
我如何在 javascript 中创建一个新事件/像在 c# 中一样? private event EventHandler asdCompleted; private void SetEventHa
我经常访问一个名为 GOOD 的网站我特别喜欢一种审美风格;导航栏如何在网站背景中扩展其颜色。如果您访问该网站,就会明白我的意思。 在 CSS 中,我怎样才能以最简单的方式复制它?我已经用 z-ind
我对 LINQ 比较陌生,不知道如何执行 Like 条件。我有一个 myObject 的 IEnumerable 列表,想要做一些类似 myObject.Description 的事情,比如“Help
我正在尝试在 Sharepoint 2013 提供商托管的应用程序中构建一个类似人员选择器的工具。最初,我使用的是 Utility.ResolvePrincipal,它让我可以访问 Sharepoin
过去几个月我一直在研究微服务架构应用程序,我仍在努力适应分布式特性。我多次注意到一种模式,但我不确定处理它的首选方式是什么。 假设我们有服务 A、服务 B 和服务 C。服务 A 公开了一个 API,其
这个问题在这里已经有了答案: Equivalent to unix "less" command within R console (5 个回答) 6年前关闭。 R 控制台中是否有任何命令与 Linu
是否可以在 Xcode 中为类似于 emacs 中的“标记”功能的行添加书签?还有我可以用来跳转到行号的快捷方式吗?我的源代码变得很长且难以导航。 最佳答案 是的;如果您将文本插入符号放在要添加书签的
在使用 vi 15 年的大部分时间后,我在使用 Go 时一时兴起尝试了 Rob Pike 的 Acme。我真的很喜欢它的小巧轻便。现代 unix 风格的东西在 Acme 中表现不佳,而 Ruby 开发
我正在寻找可以打印矩阵[1:5, 1:5] 的任何包中的函数。 head() 适用于列数较少但矩阵较大的用户。我知道我可以为它创建自己的函数,但我想知道是否已经有函数了。 最佳答案 在名为futile
我正在用 C++ 构建一个聚类算法,但我不能很好地处理 OOP 和发生变化的变量(成员数据)的状态。对于某种复杂的算法,我发现这是我发展的障碍。 因此,我正在考虑将编程语言更改为一种功能语言:Ocam
我有一个这样的日志: Jun 21 06:25:07 172.25.1.1 kernel: DROP IN=ppp0 OUT= MAC= SRC=206.221.177.2 DST=185.79.95
我需要一些帮助来制作类似于 Accordion 的东西。目标是,如果您单击导航中的链接,一个部分会消失,而您单击的部分会出现(在相同位置且不明显)。 如果可能,它还应该自动滚动到该部分的开头(导航的结
我是一名优秀的程序员,十分优秀!