• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

ios - setNeedsDisplay 未调用 drawRect :(CGRect)rect

[复制链接]
菜鸟教程小白 发表于 2022-12-13 16:19:53 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我正在尝试遵循有关如何在屏幕上绘制一些形状的指南,但在应用程序启动时它工作正常,但我无法使用 setNeedsDisplay “重新绘制”形状我'尝试了很多漂浮的东西,比如在主线程上执行,但它不起作用。

我的应用是由这个组成的:

enter image description here

我的 UIView 有它自己的类,DrawView。这是我的代码:

DrawView.h

#import <UIKit/UIKit.h>

NSInteger drawType;

@interface DrawView : UIView

-(void)drawRectCGRect)rect;
-(void)drawNowNSInteger)type;

@end

DrawView.m

#import "DrawView.h"

@implementation DrawView

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

- (void)drawRectCGRect)rect {

    CGContextRef context = UIGraphicsGetCurrentContext();
    UIColor *color = [UIColor orangeColor];
    CGContextSetStrokeColorWithColor(context, color.CGColor);
    CGContextSetFillColorWithColor(context, color.CGColor);

    NSLog(@"Type: %i",drawType);

    switch (drawType) {
            case 0:
                CGContextMoveToPoint(context, 10, 100);
                CGContextAddLineToPoint(context, 300, 300);
                CGContextSetLineWidth(context, 2.0);
                CGContextStrokePath(context);
                break;
            case 1:
                CGContextAddEllipseInRect(context,CGRectMake(10, 100, 300,440));
                CGContextDrawPath(context, kCGPathFillStroke);
                break;
            case 2:
                CGContextAddRect(context, CGRectMake(10, 100,300,300));
                CGContextDrawPath(context, kCGPathFillStroke);
                break;
            default:
                break;
    }

}


-(void)drawNowNSInteger)type {
    drawType = type;
    NSLog(@"Draw Now! %i",drawType);

    //[self setNeedsDisplay]; // Not working...
    //[self performSelectorOnMainThreadselector(setNeedsDisplay) withObject:nil waitUntilDone:YES]; // Not Working
}

@end

ViewController.h

#import <UIKit/UIKit.h>
#import "DrawView.h"

DrawView *mydraw;

@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UISegmentedControl *drawTypeSW;

- (IBAction)drawNowid)sender;
@end

ViewController.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController
@synthesize drawTypeSW;

- (void)viewDidLoad
{
    [super viewDidLoad];
    mydraw = [[DrawView alloc] init];
}

- (void)viewDidUnload
{
    [self setDrawTypeSW:nil];
    [super viewDidUnload];
}

- (BOOL)shouldAutorotateToInterfaceOrientationUIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (IBAction)drawNowid)sender {
    [mydraw drawNow:drawTypeSW.selectedSegmentIndex];
}
@end

当我打开应用程序时画了一条线,但是当我尝试使用按钮 Draw Now 绘制其他内容时,它不起作用,没有任何反应,并且 - (void)drawRect: (CGRect)rect 未被调用。为什么?我错过了什么?

谢谢;)



Best Answer-推荐答案


进一步编辑,最终解决方案:在 viewDidLoad 中创建的 DrawView 没有作为 subview 添加到 ViewController。添加以下行应使其正确显示(除了下面的其他修复):

[self.view addSubview:mydraw];

编辑:我的答案的扩展(它仍然存在,但我认为不再是问题的核心)。您正在声明界面的 myDraw outside(并在 DrawView 的头文件中执行类似的操作)。而不是

DrawView *mydraw;

@interface ViewController : UIViewController
//snip
@end

尝试:

@interface ViewController : UIViewController
{
DrawView *mydraw;
}
//snip
@end

原答案: 变量 myDraw 未正确初始化 - UIView 需要使用 initWithFrame(用于编程创建)或 initWithCoder(在 nib 或 Storyboard 中)进行初始化。使用 init 创建它意味着它的框架位置/大小为 0(并且其他 UIView 功能可能未正确初始化),这反过来意味着它不会正确绘制。

尝试 a) 在您的 nib/storyboard 中创建一个 UIView,将其转换为 DrawView,并使您的 ViewController 定义中的 drawView 字段成为该 View 的导出。或者 b) 将其从您的 nib 中移除,使用 initWithFrame 创建您的 DrawView,并为其指定屏幕上的位置和大小。

关于ios - setNeedsDisplay 未调用 drawRect CGRect)rect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12468320/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap