与Scrollview一起苦苦挣扎

时间:2014-07-17 15:44:15

标签: ios objective-c uiscrollview

我是一位试图学习新生儿的老人。这很好。

@implementation MESViewController 
- (void)viewDidLoad { 
[super viewDidLoad];  
self.view= [[QuartzLineView alloc] initWithFrame:CGRectZero];} 

-(void)Play{ 
for(int i=0;i<2000;i++){ 
short int  lineno=function result; 
CGRect  myRect=CGRectMake(0,lineno, 1024,lineno+235); 
[self.view setNeedsDisplayInRect:myRect];} 

然而,使用滚动视图,视图似乎根本不会重绘。 Scrollview显示并触摸输入确定。

#import <UIKit/UIKit.h 
@interface MESViewController : UIViewController<UIScrollViewDelegate> 
@property (nonatomic, strong) IBOutlet UIScrollView *scrollView; 
@end 

@implementation MESViewController 
- (void)viewDidLoad { 
[super viewDidLoad]; 
self.scrollView=[[UIScrollView alloc] initWithFrame: CGRectMake(0, 0, x, y)]; 
self.scrollView.contentSize=CGSizeMake(x,y); 
UIView *myview= [[QuartzLineView alloc] initWithFrame: CGRectMake(0, 0, x, y)]; 
[self.scrollView addSubview:myview]; 
self.view=self.scrollView;} 

-(void)Play{...as above} 

@implementation QuartzLineView 
- (instancetype)initWithFrame:(CGRect)r{ 
self = [super initWithFrame:CGRectMake(0, 0, x, y]; 
if (self) { 
    self.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:1.0]; 
    self.multipleTouchEnabled = YES; 
} 
return self;} 

1 个答案:

答案 0 :(得分:0)

问题在于,当您将self.view设置为滚动视图时,您仍然会调用[self.view setNeedsDisplay],它将告诉 UIScrollView 它需要显示,并且您希望告诉 QuartzLineView 它需要显示。

保存对QuartzLineView的引用,并在其上调用setNeedsDisplay应该可以解决问题:

@implementation MESViewController{
    // save a reference to the QuartzLineView
    QuartzLineView* lineView;
}
- (void)viewDidLoad { 
    [super viewDidLoad]; 
    self.scrollView=[[UIScrollView alloc] initWithFrame: CGRectMake(0, 0, x, y)]; 
    self.scrollView.contentSize=CGSizeMake(x,y); 

    // saving the reference here
    lineView= [[QuartzLineView alloc] initWithFrame: CGRectMake(0, 0, x, y)]; 
    [self.scrollView addSubview:lineView]; 
    self.view=self.scrollView;
}

-(void)Play{ 
    for(int i=0;i<2000;i++){ 
        short int  lineno=function result; 
        CGRect  myRect=CGRectMake(0,lineno, 1024,lineno+235); 

        // now tell the _lineView_ to setNeedsDisplay, instead of self.view
        [lineView setNeedsDisplayInRect:myRect];
    }
}