중요 : 나는 문제를 파악했지만 사회자는 몇 가지 이유로 삭제
:setNeedsDisplay은의 drawRect를 호출하지 않습니다
I 버튼에서 함수를 직접 호출하는 대신보기 기능을 갖는 시도 제어 장치.
아직 작동하지 않는 이유를 아직 완전히 이해하지 못해서 그 질문에 대한 답변을 수락합니다.
메인 포스트
UIButton을 누른 후에 그림을 그리는 중입니다. 내 그림은 UIView의 하위 클래스에서 수행됩니다. ViewController에이 하위 클래스의 인스턴스가 있습니다. 서브 클래스의 함수를 호출하는 ViewController에 UIButton도 있습니다. 내 응용 프로그램을로드 할 때 해당 함수에서 내가
[self setNeedsDisplay];
를 호출의 drawRect이 때보기로드에 한 번이라고하지만 setNeedsDisplay이 버튼을 누를 때마다 호출되고 있지만, 다시 호출되지 않습니다. 나는 많은 비슷한 질문을 보았고 해결책을 찾지 못했습니다.
AEViewController.h
#import <UIKit/UIKit.h>
#import "AEGraphView.h"
@interface AEViewController : UIViewController{
IBOutlet AEGraphView *theView;
}
-(IBAction)updateAEGraphView:(id)sender;
@end
AEViewController.m
#import "AEViewController.h"
@interface AEViewController()
@end
@implementation AEViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
theView = [[AEGraphView alloc] init];
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
else {
return YES;
}
}
-(IBAction)updateAEGraphView:(id)sender{
[theView UpdateView];
}
@end
AEGraphView.h
#import <UIKit/UIKit.h>
@interface AEGraphView : UIView{
CGContextRef mainContext;
int power;
int multiplier;
}
-(void)prepareGraphics;
- (void) drawLineFrom:(CGPoint)p1 To:(CGPoint)p2;
-(void)UpdateView;
@end
AEGraphView.m :
나는 여기에 내 모든 코드를 게시 할 예정입니다
#import "AEGraphView.h"
@implementation AEGraphView
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
return self;
}
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
[self prepareGraphics];
// Drawing code
NSLog(@"called");
if (power == 0) {
[self drawLineFrom:CGPointMake(100,100) To:CGPointMake(-50, -2)];
}
else {
[self drawLineFrom:CGPointMake(0, 0) To:CGPointMake(150, 150)];
}
[super drawRect:rect];
}
-(void)prepareGraphics{
mainContext = UIGraphicsGetCurrentContext();
CGContextSetShouldAntialias(mainContext, TRUE);
CGContextSetLineWidth(mainContext, 2);
CGContextSetLineCap(mainContext, kCGLineCapRound);
}
- (void) drawLineFrom:(CGPoint)p1 To:(CGPoint)p2{
NSLog(@"%f,%f to %f,%f", p1.x,p1.y,p2.x,p2.y);
// Begin a path.
CGContextBeginPath(mainContext);
// Add a line to the path.
CGContextMoveToPoint(mainContext, p1.x, p1.y);
CGContextAddLineToPoint(mainContext, p2.x, p2.y);
// Stroke and reset the path.
CGContextStrokePath(mainContext);
}
-(void)UpdateView{
if (power == 0) {
power = 1;
}
else {
power = 0;
}
[self setNeedsDisplay];
}
@end
'-drawRect'가 보이지 않으면 실행되지 않으므로보기가 표시되는지 확인하십시오. – Vervious
setNeedsDisplay가 호출 되더라도 setNeedsDisplay에 대한 호출로 끝나는 IBAction 함수가 다른 클래스에서 호출되면 drawRect가 호출되지 않습니다. . 이제 질문은 왜 IBAction을 AEGraphView에 직접 보내야합니까? – WolfLink
그런 다음 IBAction은 매번 동일한 방식으로 작동해야하며 작동해야합니다 (이는 void 유형 함수와 동일합니다). 'theView = [[AEGraphView alloc] init ;;은 IBOutlet이 올바로 연결되어 있다면 이미 삽입되어 있어야합니다.) 의심스러운 것으로 보이고, 뷰 참조를 새로운 참조로 대체 할 것입니다. 표시되고 drawRect가 호출되지 않습니다. 'NSLog ("% @", self)'를 사용하여 UIView 클래스의 올바른 인스턴스에서 메소드를 트리거하는지보십시오. – Vervious