【问题标题】:Is there a way to invert touch position in a view?有没有办法在视图中反转触摸位置?
【发布时间】:2011-11-20 18:45:27
【问题描述】:

我正在编写一个基于 CGRect 中的触摸位置更改图像和变量的方法。我目前正在使用一系列 if 语句手动反转触摸位置。我不相信这是一种特别有效的方法。

它适用于图像更改,但对于 var 更改(我还没有实现)我需要以 +1 为增量从 1 到 100 进行缩放。

- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
    UITouch *touch = [touches anyObject];

    CGPoint point = [touch locationInView: self.view];
    if (CGRectContainsPoint(CGRectMake(0, 20, 117, 200), point)) {

    if (point.y >= 20 && point.y <= 32) //12
        imageA.image = [UIImage imageNamed:@"ChemA-15.png"];
    if (point.y >= 33 && point.y <= 45)
        imageA.image = [UIImage imageNamed:@"ChemA-14.png"];
    if (point.y >= 46 && point.y <= 58)
        imageA.image = [UIImage imageNamed:@"ChemA-13.png"];
    if (point.y >= 59 && point.y <= 70)
        imageA.image = [UIImage imageNamed:@"ChemA-12.png"];
    if (point.y >= 71 && point.y <= 82)
        imageA.image = [UIImage imageNamed:@"ChemA-11.png"];
    if (point.y >= 83 && point.y <= 94)
       imageA.image = [UIImage imageNamed:@"ChemA-10.png"];
    if (point.y >= 95 && point.y <= 106)
        imageA.image = [UIImage imageNamed:@"ChemA-9.png"];
    if (point.y >= 107 && point.y <= 118)
        imageA.image = [UIImage imageNamed:@"ChemA-8.png"];
    if (point.y >= 119 && point.y <= 130)
        imageA.image = [UIImage imageNamed:@"ChemA-7.png"];
    if (point.y >= 131 && point.y <= 142)
        imageA.image = [UIImage imageNamed:@"ChemA-6.png"];
    if (point.y >= 143 && point.y <= 154)
        imageA.image = [UIImage imageNamed:@"ChemA-5.png"];
    if (point.y >= 155 && point.y <= 166)
        imageA.image = [UIImage imageNamed:@"ChemA-4.png"];
    if (point.y >= 167 && point.y <= 178)
       imageA.image = [UIImage imageNamed:@"ChemA-3.png"];
    if (point.y >= 179 && point.y <= 190)
        imageA.image = [UIImage imageNamed:@"ChemA-2.png"];
    if (point.y >= 191 && point.y <= 202)
        imageA.image = [UIImage imageNamed:@"ChemA-1.png"];

    }
}

有没有更好的方法来做到这一点?感谢您提供的任何帮助。

【问题讨论】:

    标签: iphone objective-c touchesbegan touchesmoved


    【解决方案1】:

    你可以计算出你需要什么样的图片:

    int image = 15 - (int)((point.y - 20) / 13);
    

    然后从中创建一个字符串并检查以下边界:

    if(image >= 1 && image <= 15)
        imageA.image = [UIImage imageNamed:
                        [NSString stringWithFromat: @"ChemA-%d.png", image]];
    

    【讨论】:

    • 哦,这是一种非常有趣的方式。从最大值开始,然后带走位置。永远不会想到这一点。杰出的。谢谢!
    猜你喜欢
    • 2019-03-05
    • 1970-01-01
    • 1970-01-01
    • 2012-08-08
    • 1970-01-01
    • 1970-01-01
    • 2010-10-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多