子类化 UIScrollview

发布于 2025-01-01 04:50:38 字数 1653 浏览 2 评论 0原文

我一直在拼命地尝试将一些图像绘制到视图中。该视图应该位于滚动视图内。为此,我对 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];
}
}

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

I have been trying desperately to draw some images into a view. The view should be inside a scrollview. For that I subclassed UIScrollview and override the drawRect method in it. And added this as my UIView's subview.

@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];
}
}

Am I missing something here. Is this the right way to go.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

任性一次 2025-01-08 04:50:38

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

If the view that should perform the drawing resides in that UIScrollView, you have to put the - (void)drawRect:(CGRect)rect method into that view's class method and not into the UIScrollView subclass.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文