【问题标题】:setNeedsDisplay has not been called尚未调用 setNeedsDisplay
【发布时间】:2013-04-23 12:43:14
【问题描述】:

我使用drawrectUIView 上绘制一个正弦波,它链接到UIViewController。 当我第一次加载视图控制器时,它可以绘制一次。 我希望当我在ViewController 中更改UISlider 的值时,它会更改正弦波的频率。 我在IBAction 中使用了setNeedsDisplay 函数,但它不起作用。 这是我的代码

//CROO.m
#import "CROO.h"

@implementation CROO
@synthesize frequency;
CGFloat fffff;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.frequency = fffff;
        [self setNeedsDisplay];
        // Initialization code
    }
    return self;

}

-(void)drawRect:(CGRect)rect
{

    CGContextRef context = UIGraphicsGetCurrentContext();

    CGContextSetLineWidth(context, 1);
    CGContextSetLineJoin(context, kCGLineJoinRound);
    NSLog(@"hi i am here %f",fffff);
    const CGFloat amplitude = 200/2;
    for(CGFloat x = 0; x < 600; x += 0.5)
    {
        CGFloat y = amplitude * cosf(2 * M_PI * (x / 600) * fffff) + 200;
        if(x == 0)
            CGContextMoveToPoint(context, x, y);
        else
            CGContextAddLineToPoint(context, x, y);
    }
    CGContextSetStrokeColorWithColor(context, [[UIColor greenColor] CGColor]);
    self.clearsContextBeforeDrawing = NO;

    CGContextStrokePath(context);
}

-(void)setFrequency:(CGFloat)f
{
    frequency = f;
    [self hello];
    NSLog(@"fre = %f",fffff);
}
-(void)hello
{
    fffff = frequency;
    [self setNeedsDisplay];
}
@end
//CosViewController.m
#import "CosViewController.h"
@interface CosViewController ()
@end

@implementation CosViewController
@synthesize frequency,currfrelab;
@synthesize cro;
float tempfreq;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //cro = [[CROO alloc]initWithFrame:cro.frame];
    cro = [[CROO alloc]initWithFrame:cro.frame];

    //[self.view addSubview:cro];
    [cro setFrequency:frequency.value];
    //[self performSelectorOnMainThread:@selector(setNeedsDisplay) withObject:nil waitUntilDone:YES];
    NSLog(@"freq = %f",cro.frequency);
    currfrelab.text = [NSString stringWithFormat:@"Frequency = %f",frequency.value];
    //[self.view setNeedsDisplayInRect:cro.frame];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(IBAction)updatefreq:(id)sender
{
   // cro = [[CROO alloc]init];
    CGFloat freq = [(UISlider *)sender value];
   // [self.view addSubview:cro];
    [cro setNeedsDisplay];
    //[[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode beforeDate: [NSDate date]];
    currfrelab.text = [NSString stringWithFormat:@"Frequency = %f",freq];
    //[self.view performSelectorOnMainThread:@selector(setNeedsDisplay) withObject:nil waitUntilDone:YES];

}
@end

【问题讨论】:

  • PS它可以使用初始滑块值的数据来生成波形。以便CROO类可以获取值。

标签: ios objective-c xcode drawrect setneedsdisplay


【解决方案1】:

init 只被调用一次,init。从另一个控制器调用 setNeedsDisplay 以再次弹出 drawRect。

【讨论】:

  • 那我应该改变什么?
  • 我已经添加了一个函数来测试 setNeedsDisplay 但它仍然没有工作。 -(IBAction)testing:(id)sender { [cro setFrequency:10]; [cro setNeedsDisplay]; } 我点击一个按钮并调用这个函数
  • 您是否尝试从同一视图调用 setNeedsDisplay?您需要从您的 viewController 调用它。类似 [otherView setNeedsDisplay];
  • 如果这不起作用,那么问题在于您如何创建要重绘的 UIView 的实例。为其创建一个属性,然后对该属性调用 setNeedsDisplay。
  • 如何为其创建属性?
【解决方案2】:
-(IBAction)updatefreq:(id)sender
{
   // cro = [[CROO alloc]init];
    CGFloat freq = [(UISlider *)sender value];
    **[cro setFrequency:freq];**
   // [self.view addSubview:cro];
    [cro setNeedsDisplay];
    //[[NSRunLoop currentRunLoop] runMode: NSDefaultRunLoopMode beforeDate: [NSDate date]];
    currfrelab.text = [NSString stringWithFormat:@"Frequency = %f",freq];
    //[self.view performSelectorOnMainThread:@selector(setNeedsDisplay) withObject:nil waitUntilDone:YES];

}

【讨论】:

  • 是drawrect中正确出现的频率值...因为只有这样您才会看到正弦波变化...
  • 我已经检查过UIView类中的值频率和fffff发生了变化。但是它不能进入​​drawrect......我该怎么办?
  • cro = [[CROO alloc]initWithFrame:cro.frame];在这里,您的 cro 对象尚未初始化,您仍在提供 cro.frame... 使其如下所示 cro = [[CROO alloc]initWithFrame:CGRectMake(rect you want)];
  • (你要的rect)应该是cro uiview的大小吧?顺便说一句,当我使用 cro = [[CROO alloc]init]; 时它仍然不起作用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-18
相关资源
最近更新 更多