有没有办法反转视图中的触摸位置?
我正在编写一种根据 CGRect 中的触摸位置更改图像和 var 的方法。我目前正在使用一系列 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"];
}
}
有更好的方法吗?感谢您提供的任何帮助。
I'm writing a method that changes an image and a var based on a touch position in an CGRect. I am currently manually inverting the touch position using a series of if statements. I'm not convinced this is a particularly efficient method of doing this.
It works ok for the image changing, but for the var changing (which I haven't implemented yet) I would need to scale from 1 – 100 in +1 increments.
- (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"];
}
}
Is there a better way to do this? Thanks for any help offered.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以计算所需的图像,例如:
然后从中创建一个字符串并检查边界,例如:
You can calculate what image you need like:
and then make a string from it and check for bounds like: