【问题标题】:Draw lines a touches event not work perfectly on FullScreen mode in iphone绘制线条触摸事件在 iphone 的全屏模式下无法正常工作
【发布时间】:2012-04-19 18:53:13
【问题描述】:

我正在创建应用程序以在 iphone 上成功绘图。但是,它的工作只设置状态栏最初隐藏 = 否在 myapp-info.plist.else 如果,设置状态栏最初隐藏=是的,不能完美工作。我的视图控制器代码如下:

- (void)viewDidLoad {
    [super viewDidLoad];
    drawImage = [[UIImageView alloc] initWithImage:nil];
    drawImage.frame = self.view.frame;
    drawImage.backgroundColor=[UIColor blackColor];
    [self.view addSubview:drawImage];
    mouseMoved = 0;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {

    mouseSwiped = NO;
    UITouch *touch = [touches anyObject];

    if ([touch tapCount] == 2) {
        drawImage.image = nil;
        return;
    }

    lastPoint = [touch locationInView:self.view];
    //lastPoint.y -= 20;

}

-(void)viewDidAppear:(BOOL)animated
{
 // [UIApplication sharedApplication].statusBarHidden = NO;
}

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    mouseSwiped = YES;
    UITouch *touch = [touches anyObject];   
    CGPoint currentPoint = [touch locationInView:self.view];
    currentPoint.y -= 20; // only for 'kCGLineCapRound'
    UIGraphicsBeginImageContext(self.view.frame.size);
    //Albert Renshaw - Apps4Life
    [drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)]; //originally self.frame.size.width, self.frame.size.height)];
    CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); //kCGLineCapSquare, kCGLineCapButt, kCGLineCapRound
    CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0); // for size
    CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 1.0, 0.0, 1.0); //values for R, G, B, and Alpha
    CGContextBeginPath(UIGraphicsGetCurrentContext());
    CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
    CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
    CGContextStrokePath(UIGraphicsGetCurrentContext());
    drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();

    lastPoint = currentPoint;

    mouseMoved++;

    if (mouseMoved == 10) {
        mouseMoved = 0;
    }

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {

    UITouch *touch = [touches anyObject];

    if ([touch tapCount] == 2) {
        drawImage.image = nil;
        return;
    }
    if(!mouseSwiped) {
        //if color == green
        UIGraphicsBeginImageContext(self.view.frame.size);
        [drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)]; //originally self.frame.size.width, self.frame.size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); //kCGLineCapSquare, kCGLineCapButt, kCGLineCapRound
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10.0);
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 1.0, 0.0, 1.0);
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        CGContextFlush(UIGraphicsGetCurrentContext());
        drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
}

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];

    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [self setDrawImage:nil];
    [self setMy_view:nil];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}


- (void)dealloc {
    [my_view release];
    [drawImage release];
    [super dealloc];
}

此代码在没有全屏模式的情况下完美运行。否则,无法像下面的输出一样工作:

请任何人帮我提供任何代码或示例...!

谢谢..!

【问题讨论】:

    标签: iphone ios


    【解决方案1】:

    将 drawImage.frame = self.view.frame; 从
    - (void)viewDidLoad 方法
    移动到
    -(void)viewWillAppear:(BOOL)animated
    应在 viewWillAppear 或 viewDidAppear 中进行任何几何调整。

    【讨论】:

    • 谢谢...!它的工作就像 Charm 的...!
    【解决方案2】:

    我终于在我的控制器类下面找到了解决方案:

    - (void)viewDidLoad {
        [super viewDidLoad];
    
    }
    
    -(void)viewWillAppear:(BOOL)animated
    {
        drawImage = [[UIImageView alloc] initWithImage:nil];
        drawImage.frame = self.view.frame;
        drawImage.backgroundColor=[UIColor blackColor];
        drawImage.center=self.view.center;
        [self.view addSubview:drawImage];
        mouseMoved = 0;
    }
    
    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    
        mouseSwiped = NO;
        UITouch *touch = [touches anyObject];
    
        if ([touch tapCount] == 2) {
            drawImage.image = nil;
            return;
        }
    
        lastPoint = [touch locationInView:self.view];
        lastPoint.y -= 0;
    
    }
    
    
    - (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
        mouseSwiped = YES;
        UITouch *touch = [touches anyObject];   
        CGPoint currentPoint = [touch locationInView:self.view];
        //currentPoint.y += 20; // only for 'kCGLineCapRound'
        UIGraphicsBeginImageContext(self.view.frame.size);
        //Albert Renshaw - Apps4Life
        [drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)]; //originally self.frame.size.width, self.frame.size.height)];
        CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); //kCGLineCapSquare, kCGLineCapButt, kCGLineCapRound
        CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 5.0); // for size
        CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 1.0, 0.0, 1.0); //values for R, G, B, and Alpha
        CGContextBeginPath(UIGraphicsGetCurrentContext());
        CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
        CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), currentPoint.x, currentPoint.y);
        CGContextStrokePath(UIGraphicsGetCurrentContext());
        drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    
        lastPoint = currentPoint;
    
        mouseMoved++;
    
        if (mouseMoved == 10) {
            mouseMoved = 0;
        }
    
    }
    
    - (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
    
        UITouch *touch = [touches anyObject];
    
        if ([touch tapCount] == 2) {
            drawImage.image = nil;
            return;
        }
        if(!mouseSwiped) {
            //if color == green
            UIGraphicsBeginImageContext(self.view.frame.size);
            [drawImage.image drawInRect:CGRectMake(0, 0, drawImage.frame.size.width, drawImage.frame.size.height)]; //originally self.frame.size.width, self.frame.size.height)];
            CGContextSetLineCap(UIGraphicsGetCurrentContext(), kCGLineCapRound); //kCGLineCapSquare, kCGLineCapButt, kCGLineCapRound
            CGContextSetLineWidth(UIGraphicsGetCurrentContext(), 10.0);
            CGContextSetRGBStrokeColor(UIGraphicsGetCurrentContext(), 0.0, 1.0, 0.0, 1.0);
            CGContextMoveToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
            CGContextAddLineToPoint(UIGraphicsGetCurrentContext(), lastPoint.x, lastPoint.y);
            CGContextStrokePath(UIGraphicsGetCurrentContext());
            CGContextFlush(UIGraphicsGetCurrentContext());
            drawImage.image = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
        }
    }
    - (void)didReceiveMemoryWarning {
        // Releases the view if it doesn't have a superview.
        [super didReceiveMemoryWarning];
    
        // Release any cached data, images, etc that aren't in use.
    }
    
    - (void)viewDidUnload {
        [self setDrawImage:nil];
        [self setMy_view:nil];
        // Release any retained subviews of the main view.
        // e.g. self.myOutlet = nil;
    }
    
    
    - (void)dealloc {
        [my_view release];
        [drawImage release];
        [super dealloc];
    }
    

    【讨论】:

    • 您可以在 viewDidLoad 方法中保留初始化,因为它是进行这些自定义初始化的最佳位置。 viewWillAppear 方法是您进行轻量级调整的最后机会,并且在此方法的范围内,视图层次结构的所有几何形状都已正确设置,因此请将其用于快速操作(例如设置框架、中心或其他一些小东西)
    • 我不知道它是否对速度很重要(我很想知道)但为了代码清晰,您可能希望将上下文存储在局部变量中,而不是每个都调用它时间(例如CGContextRef context = UIGraphicsGetCurrentContext();)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多