Objective c 自定义视图图形怪异
我试图创建自己的自定义UIProgressView,方法是将其子类化,然后覆盖drawRect函数。 除进度填充条外,所有操作均按预期进行。我不能把高度和图像弄对 图像均为视网膜分辨率,模拟器处于视网膜模式。 这些图像称为:progressBar@2x.png“(28px高)和”progressBarTrack@2x.png“(32px高) CustomProgressView.hObjective c 自定义视图图形怪异,objective-c,ios,drawrect,cgrectmake,Objective C,Ios,Drawrect,Cgrectmake,我试图创建自己的自定义UIProgressView,方法是将其子类化,然后覆盖drawRect函数。 除进度填充条外,所有操作均按预期进行。我不能把高度和图像弄对 图像均为视网膜分辨率,模拟器处于视网膜模式。 这些图像称为:progressBar@2x.png“(28px高)和”progressBarTrack@2x.png“(32px高) CustomProgressView.h #import <UIKit/UIKit.h> @interface CustomProgressV
#import <UIKit/UIKit.h>
@interface CustomProgressView : UIProgressView
@end
生成的ProgressView具有正确的高度和宽度。它还以正确的百分比填充(当前设置为80%)。但是没有正确绘制进度填充图像
有人看到我哪里出错了吗
看起来您正在
-drawRect
中重新分配self.frame
我想你想要这样的东西:
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
// Drawing code
CGRect bounds = self.bounds ;
UIImage *progressBarTrack = [ UIImage imageNamed:@"progressBarTrack"] ;
[ progressBarTrack drawInRect:bounds ] ;
UIImage *progressBar = [[UIImage imageNamed:@"progressBar"] resizableImageWithCapInsets:(const UIEdgeInsets){ 4.0f, 4.0f, 5.0f, 4.0f } ] ;
CGRect fillRect = CGRectInset( bounds, 2.0f, 2.0f ) ;
fillRect.width = floorf( self.progress * maximumWidth );
[progressBar drawInRect:fillRect];
}
如何创建自己的进度视图覆盖UIView而不是UIProgressView
@interface ProgressView : UIView
@property float progress ;
@end
@implementation ProgressView
@synthesize progress = _progress ;
-(id)initWithFrame:(CGRect)frame
{
if (( self = [ super initWithFrame:frame ] ))
{
self.layer.needsDisplayOnBoundsChange = YES ;
}
return self ;
}
-(void)drawRect
{
// see code above
}
-(void)setProgress:(float)progress
{
_progress = progress ;
[ self setNeedsDisplay ] ;
}
@end
我这样做是为了让ProgressView高出16点,而不是默认的9点。我在你的例子里找不到。哦,对了。不过你不应该在这里这么做。我会将
clipstobunds
设置为NO
,或者只是子类UIView
,而不是UIProgressView
。您正在与uiprogressview
的内置实现作斗争。在这种情况下,我只做我自己的。此外,如果将来的iOS对其实现等进行了更改,您可能会遇到麻烦。子类UIView?这不是很一般吗?我只想更改progressview。或者我不明白你的意思。你能给我举个例子吗?我贴了个例子。很简单!
@interface ProgressView : UIView
@property float progress ;
@end
@implementation ProgressView
@synthesize progress = _progress ;
-(id)initWithFrame:(CGRect)frame
{
if (( self = [ super initWithFrame:frame ] ))
{
self.layer.needsDisplayOnBoundsChange = YES ;
}
return self ;
}
-(void)drawRect
{
// see code above
}
-(void)setProgress:(float)progress
{
_progress = progress ;
[ self setNeedsDisplay ] ;
}
@end