iOS如何实现水平缩放视图?
How to achieve horizontal scaling view in iOS?
我已经使用 TSRDialScrollView 库来按照我的代码中的设计要求进行操作。
但解决不了几个问题。
引用自下方 link :
https://www.cocoacontrols.com/controls/trsdialscrollview
我对我的代码做了一些改动
-> 清除背景颜色
CGContextRef context = UIGraphicsGetCurrentContext();
[[UIColor clearColor] set];
CGContextFillRect(context, rect);
但是显示的是黑色
-> 视图反转后设置标签
- (void)drawLabelWithContext:(CGContextRef)context
atPoint:(CGPoint)point
text:(NSString *)text
fillColor:(UIColor *)fillColor
strokeColor:(UIColor *)strokeColor {
// We want the label to be centered on the specified x value
NSInteger label_y = ((point.y-100) - (boundingBox.width / 2));
}
-> 通过在代码中将次要和主要刻度长度更改为负值来反转整个视图。
- (void)drawTicksWithContext:(CGContextRef)context atX:(long)x
{
CGPoint point = CGPointMake(x, self.frame.size.height);
CGContextSetShadowWithColor(
context,
self.shadowOffset,
self.shadowBlur,
self.shadowColor.CGColor);
if ([self isMajorTick:x]) {
[self drawMajorTickWithContext:context
atPoint:point
withColor:self.majorTickColor
width:self.majorTickWidth
length:-self.majorTickLength];
// Draw the text
//
// 1) Take the existing position and subtract off the lead spacing
// 2) Divide by the minor ticks to get the major number
// 3) Add the minimum to get the current value
//
int value = (point.x - self.leading) / self.minorTickDistance + _minimum;
NSString *text = [NSString stringWithFormat:@"%i", value];
[self drawLabelWithContext:context
atPoint:point
text:text
fillColor:self.labelFillColor
strokeColor:self.labelStrokeColor];
} else {
CGContextSaveGState(context);
[self drawMinorTickWithContext:context
atPoint:point
withColor:self.minorTickColor
width:self.minorTickWidth
length:-self.minorTickLength];
// Restore the context
CGContextRestoreGState(context);
}
}
我想按如下所示进行缩放。
1)通过调整偏移量,使scrollview的view居中(即滚动到最小值时0的位置不在view的中间)
2) 我希望背景颜色是清晰的颜色。
3) 根据设计如何将红色设置为固定的中间线?
4) 如何在中间显示如图所示的浅灰色?
感谢您的帮助,请建议如何实现这一点,我真的需要完成这个非常需要的任务。
提前致谢。
TRSDialView.m
文件:-
在- (instancetype)initWithFrame:(CGRect)frame
中添加self.opaque = NO;
在 - (void)drawRect:(CGRect)rect
方法中,在 CGContextRef context = UIGraphicsGetCurrentContext();
行
代码下方添加以下代码
// Fill the background
CGContextSetFillColorWithColor(context, self.backgroundColor.CGColor);
// CGContextFillRect(context, rect);
CGContextClearRect(context, rect);
以上代码会清除背景颜色。
要调整 offset
添加 width constraint
到 TRSDialView
in Storyboard
可以在storyboard
中添加红线和灰视图,如subview
我已经使用 TSRDialScrollView 库来按照我的代码中的设计要求进行操作。
但解决不了几个问题。
引用自下方 link :
https://www.cocoacontrols.com/controls/trsdialscrollview
我对我的代码做了一些改动
-> 清除背景颜色
CGContextRef context = UIGraphicsGetCurrentContext();
[[UIColor clearColor] set];
CGContextFillRect(context, rect);
但是显示的是黑色
-> 视图反转后设置标签
- (void)drawLabelWithContext:(CGContextRef)context
atPoint:(CGPoint)point
text:(NSString *)text
fillColor:(UIColor *)fillColor
strokeColor:(UIColor *)strokeColor {
// We want the label to be centered on the specified x value
NSInteger label_y = ((point.y-100) - (boundingBox.width / 2));
}
-> 通过在代码中将次要和主要刻度长度更改为负值来反转整个视图。
- (void)drawTicksWithContext:(CGContextRef)context atX:(long)x
{
CGPoint point = CGPointMake(x, self.frame.size.height);
CGContextSetShadowWithColor(
context,
self.shadowOffset,
self.shadowBlur,
self.shadowColor.CGColor);
if ([self isMajorTick:x]) {
[self drawMajorTickWithContext:context
atPoint:point
withColor:self.majorTickColor
width:self.majorTickWidth
length:-self.majorTickLength];
// Draw the text
//
// 1) Take the existing position and subtract off the lead spacing
// 2) Divide by the minor ticks to get the major number
// 3) Add the minimum to get the current value
//
int value = (point.x - self.leading) / self.minorTickDistance + _minimum;
NSString *text = [NSString stringWithFormat:@"%i", value];
[self drawLabelWithContext:context
atPoint:point
text:text
fillColor:self.labelFillColor
strokeColor:self.labelStrokeColor];
} else {
CGContextSaveGState(context);
[self drawMinorTickWithContext:context
atPoint:point
withColor:self.minorTickColor
width:self.minorTickWidth
length:-self.minorTickLength];
// Restore the context
CGContextRestoreGState(context);
}
}
我想按如下所示进行缩放。
1)通过调整偏移量,使scrollview的view居中(即滚动到最小值时0的位置不在view的中间)
2) 我希望背景颜色是清晰的颜色。
3) 根据设计如何将红色设置为固定的中间线?
4) 如何在中间显示如图所示的浅灰色?
感谢您的帮助,请建议如何实现这一点,我真的需要完成这个非常需要的任务。
提前致谢。
TRSDialView.m
文件:-
在- (instancetype)initWithFrame:(CGRect)frame
中添加self.opaque = NO;
在 - (void)drawRect:(CGRect)rect
方法中,在 CGContextRef context = UIGraphicsGetCurrentContext();
行
// Fill the background
CGContextSetFillColorWithColor(context, self.backgroundColor.CGColor);
// CGContextFillRect(context, rect);
CGContextClearRect(context, rect);
以上代码会清除背景颜色。
要调整 offset
添加 width constraint
到 TRSDialView
in Storyboard
可以在storyboard
subview