2014-07-17 41 views
-1

我是一位老人,試圖學習新的。這工作正常。與Scrollview拼搏

@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;} 
+0

'#進口

+1

問題不清楚,你能解釋一下你想達到什麼嗎? – Suryakant

+0

第二個代碼段的第一部分包含\t #import marksmithnottingham

回答

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]; 
    } 
}