子类化UIScrollview

时间:2012-01-31 12:45:43

标签: iphone uiview uiscrollview subclass

我一直在拼命想把一些图像画成视图。视图应该在scrollview中。为此,我将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];
}
}

我在这里遗漏了什么。这是正确的方法。

1 个答案:

答案 0 :(得分:0)

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