具有自定义高度的iOS drawRect
我已经在viewController上放置了一个视图,我想在此视图中绘制一个矩形,但具有自定义高度。我根据viewController中的参数确定高度。 比如说。如果我的参数是50,我希望矩形的高度为UIView的50% 2个问题:具有自定义高度的iOS drawRect,ios,height,drawrect,Ios,Height,Drawrect,我已经在viewController上放置了一个视图,我想在此视图中绘制一个矩形,但具有自定义高度。我根据viewController中的参数确定高度。 比如说。如果我的参数是50,我希望矩形的高度为UIView的50% 2个问题: 如何将高度传递给自定义drawRect 如何将矩形放置在UIView的底部 我使用Interface Builder放置了视图,并在UIView的子类中实现了drawRect,并将其用作UIView的自定义类 因此,在自定义类中,我有: - (void)draw
- 如何将高度传递给自定义drawRect
- 如何将矩形放置在UIView的底部
- (void)drawRect:(CGRect)rect
{
// Drawing code
CGContextRef context = UIGraphicsGetCurrentContext();
UIColor * lightGrayColor = [UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0];
CGRect paperRect = self.bounds;
drawLinearGradient(context, paperRect, lightGrayColor.CGColor, [UIColor clearColor].CGColor);
}
void drawLinearGradient(CGContextRef context, CGRect rect, CGColorRef startColor, CGColorRef endColor)
{
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat locations[] = { 0.0, 1.0 };
NSArray *colors = @[(__bridge id) startColor, (__bridge id) endColor];
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, (__bridge CFArrayRef) colors, locations);
CGPoint startPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMinY(rect));
CGPoint endPoint = CGPointMake(CGRectGetMidX(rect), CGRectGetMaxY(rect));
CGContextSaveGState(context);
CGContextAddRect(context, rect);
CGContextClip(context);
CGContextDrawLinearGradient(context, gradient, startPoint, endPoint, 0);
CGContextRestoreGState(context);
CGGradientRelease(gradient);
CGColorSpaceRelease(colorSpace);
}
这在我的视图中绘制了一个很好的渐变矩形,但它填充了整个视图。
这是因为自我约束
我已经在自定义类中添加了属性*height,但我不知道如何从我的viewController中填充它。因此,我希望它从UIView的底部开始,并使其达到我确定的高度(实际高度的百分比)
有人知道我如何做到这一点吗?我想你可以计算屏幕高度的50%,然后从全屏高度中减去新的视图高度
youParameter = 50; // Lets assume this is your parametere
int heightOfView = ([UIScreen mainScreen].bounds.size.height * yourParameter) / 100;
// For placing the view to the bottom;
CGRect newFrame;
frame.origin.y = [UIScreen mainScreen].bounds.size.height - heightOfView;
frame.origin.x = 0;
frame.size.width = [UIScreen mainScreen].bounds.size.width; // assuming it will take full width
frame.size.height = heightOfView;
youViewToChange.frame = newFrame; // normally do this or
要将值传递给drawRect,可以执行以下操作:
[self drawRect:newFrame];
是否在interface builder的identity inspector中设置了自定义视图类 可以从viewController中设置“高度”属性:
((MyViewClass *) self.myView).height = myCalculatedValue;
然后实现drawRect:
- (void)drawRect:(CGRect)rect
{
// Drawing code
CGContextRef context = UIGraphicsGetCurrentContext();
UIColor * lightGrayColor = [UIColor colorWithRed:230.0/255.0 green:230.0/255.0 blue:230.0/255.0 alpha:1.0];
CGRect paperRect = self.bounds;
paperRect.origin.y = paperRect.size.height - self.height;
paperRect.size.height = self.height;
//assuming your height property is of CGFloat type
drawLinearGradient(context, paperRect, lightGrayColor.CGColor, [UIColor clearColor].CGColor);
}
这将从底部上方的(高度)点到底部绘制渐变为什么不添加一个子视图(当前视图的50%),然后填充它?嗯。。当我将其放置在UIViewController中时,这会使视图变小,但我希望视图保持完整高度,但在该视图的内部(或顶部)绘制一个矩形。当我把它放在drawRect方法中时,矩形有了新的高度,但是我没有;我的情人不在那里…布莱尔雅特!我已经在inspector中设置了自定义视图类,但是我不知道(MyViewClass*)的添加。。。非常感谢你!!!不客气!如果IBOutlet的类型设置为MyViewClass,则不需要typecast。啊。。。。这是我的错误!!!我将其设置为UIView,而不是CustomView。。。请给我这张的双倍积分!