此前我问过一个在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
这是一个非常开放的问题。尝试具体并解释为什么它不起作用。例如,'changefreq:'曾经运行过吗?是否更改:附加到某个控件(如按钮)? – 2013-04-29 15:28:03
是的,它的工作原理... – IvyBB 2013-04-29 16:14:01