【问题标题】:Subclassing UIScrollview子类化 UIScrollview
【发布时间】:2012-01-31 12:45:42
【问题描述】:

我一直在拼命地尝试将一些图像绘制到视图中。视图应该在滚动视图内。为此,我将 UIScrollview 子类化并覆盖其中的 drawRect 方法。并将其添加为我的 UIView 的子视图。

@interface DrawAnotherViewClass : UIScrollView<UIScrollViewDelegate> {

}
@end



@implementation DrawAnotherViewClass

- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
    // Initialization code

    CGRect fullScreenRect=[[UIScreen mainScreen] applicationFrame];
    self.frame = fullScreenRect;
    self.contentSize = CGSizeMake(600, 600);
    self.showsHorizontalScrollIndicator = YES;
    self.showsVerticalScrollIndicator = NO;
    self.pagingEnabled = YES;

}
return self;
}



- (void)drawRect:(CGRect)rect
{
// Drawing code

CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(context, 2.0); 
CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor); 
CGContextMoveToPoint(context, 10.0f, 50.0f); 
CGContextAddLineToPoint(context, 10.0f, 200.0f);
CGContextStrokePath(context);

CGContextMoveToPoint(context, 8.0f, 77.0f); 
CGContextAddLineToPoint(context, 300.0f, 77.0f);
CGContextStrokePath(context);

CGContextSetRGBFillColor(context, 0, 0, 255, 0.1);
CGContextSetRGBStrokeColor(context, 0, 0, 255, 1);
CGContextStrokeEllipseInRect(context, CGRectMake(65.0, 33.5, 25, 25));

UIImage *image1 = [UIImage imageNamed:@"PinDown1.png"];
UIImage *image2 = [UIImage imageNamed:@"pinGreen_v1.png"];

CGPoint drawPoint = CGPointMake(0.0f, 10.0f); 
[image2 drawAtPoint:drawPoint];

for(int i =1; i<20; i++){
    CGPoint drawPointOne = CGPointMake(40.0f * i, 40.0f); 
    [image1 drawAtPoint:drawPointOne];
}
}

我在这里错过了什么吗?这是正确的方法吗?

【问题讨论】:

    标签: iphone uiview uiscrollview subclass


    【解决方案1】:

    如果应该执行绘图的视图驻留在该 UIScrollView 中,则必须将 - (void)drawRect:(CGRect)rect 方法放入该视图的类方法中,而不是放入 UIScrollView 子类中。

    【讨论】:

    • 我应该从哪里添加这个视图作为滚动视图的子视图?
    • 你可以通过 ViewController 的 viewDidLoad 方法来做到这一点。
    猜你喜欢
    • 1970-01-01
    • 2012-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-25
    • 1970-01-01
    相关资源
    最近更新 更多