2013-04-29 57 views
-3

此前我问过一个在stackoverflow中的问题,即约setNeedsDisplay无法正常工作。 我不是一个懒惰的家伙,我已经尝试了一切,但它仍然无法工作。 也许我找不到问题在哪里。 任何人都可以帮助我找出问题吗?setNeedDisplay仍然不起作用

//viewcontroller.m 
#import "ViewController.h" 
@interface ViewController() 

@end 

@implementation ViewController 
@synthesize frequency; 
- (void)viewDidLoad 
{ 
    hi = [[cro alloc]init]; 
    [self.view addSubview:hi]; 
    CGFloat fu = frequency.value; 
    [hi changefreq:fu]; 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 
-(IBAction)change:(id)sender 
{ 
    CGFloat fu = frequency.value; 
    [hi changefreq:fu]; 
} 
@end 
//cro.m 
#import "cro.h" 

@implementation cro 
@synthesize fffff; 
CGFloat freee; 
- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     // 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) * freee) + 200; 
     if(x == 0) 
      CGContextMoveToPoint(context, x, y); 
     else 
      CGContextAddLineToPoint(context, x, y); 
    } 
    CGContextSetStrokeColorWithColor(context, [[UIColor greenColor] CGColor]); 
    self.clearsContextBeforeDrawing = NO; 
    CGContextStrokePath(context); 
} 
-(void)changefreq:(CGFloat)fre 
{ 
    NSLog(@"fre= %f",fre); 
    freee = fre; 
    [self setNeedsDisplay]; 
} 

@end 

这里是项目 http://www.sendspace.com/file/lzt4b0

+1

这是一个非常开放的问题。尝试具体并解释为什么它不起作用。例如,'changefreq:'曾经运行过吗?是否更改:附加到某个控件(如按钮)? – 2013-04-29 15:28:03

+0

是的,它的工作原理... – IvyBB 2013-04-29 16:14:01

回答

2

您初始化通过调用[[cro alloc] init]你的看法。由于您不打电话给initWithFrame:,这将导致宽度和高度都为零的视图。在这样的视图上调用setNeedsDisplay没有效果,因为没有任何显示。

改变你在viewDidLoad的东西第一线像

hi = [[cro alloc] initWithFrame:CGRectMake(0, 0, 320, 320)]; 

(根据需要调整大小)

或者,你可能想使用你已经在你的故事板,而不是cro实例实例化一个新的。故事板实例就是您所看到的实例,hi实例在您当前的代码中实际上是不可见的。 (顺便说一句,如果你希望其他人阅读你的代码,你可能要开始使用合理的变量和类的名称。)

+0

OMG I成功我爱你<3 – IvyBB 2013-04-29 16:17:24

+0

我可以成功更新拉伸,但旧图像没有清理。如何清理视图? – IvyBB 2013-04-29 16:27:01

+0

删除'self.clearsContextBeforeDrawing = NO;'行。 – omz 2013-04-29 17:43:33

0

在ViewController.m viewDidLoad方法以下更改会做修复

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
    hi = [[cro alloc]initWithFrame:CGRectMake(201, 58, 414, 571)]; 
    [self.view addSubview:hi]; 
    CGFloat fu = frequency.value; 
    [hi changefreq:fu]; 
} 
0

如果下面的工作对我来说:

  • 使用属性,而不是伊娃现在
    • 您通过访问cro实例
    • 在故事板视图控制器的出口 cro(已经有您上传的项目命名为这样的属性)与子视图
  • -[ViewController viewDidLoad:]删除
  • 连接,您创建一个新的cro实例,将线它作为一个-[cro changefreq:]子视图self
  • 使用您cro -class伊娃fffff-[cro drawRect:]

(这是所有从您上传的项目中复制出来的只有我的更改)

@interface ViewController : UIViewController 

@property (strong, nonatomic) IBOutlet cro *cro; 
@property (strong, nonatomic) IBOutlet UISlider *frequency; 
-(IBAction)change:(id)sender; 
@end 

@implementation ViewController 
@synthesize frequency; 
- (void)viewDidLoad 
{ 
    CGFloat fu = frequency.value; 
    [self.cro changefreq:fu]; 
    //NSLog(@"%f",hi.fffff); 
    [super viewDidLoad]; 
    // Do any additional setup after loading the view, typically from a nib. 
} 

- (void)didReceiveMemoryWarning 
{ 
    [super didReceiveMemoryWarning]; 
    // Dispose of any resources that can be recreated. 
} 
-(IBAction)change:(id)sender 
{ 
    CGFloat fu = frequency.value; 
    [self.cro changefreq:fu]; 
    //[hi setNeedsDisplay]; 
} 
@end 

@interface cro : UIView 
@property (nonatomic) CGFloat fffff; 
-(void)changefreq:(CGFloat)fre; 
@end 

@implementation cro 
@synthesize fffff; 
- (id)initWithFrame:(CGRect)frame 
{ 
    self = [super initWithFrame:frame]; 
    if (self) { 
     fffff = 15; 
     // 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)changefreq:(CGFloat)fre 
{ 
    NSLog(@"fre= %f",fre); 
    fffff = fre; 
    [self setNeedsDisplay]; 
} 

@end 
相关问题