我在可可触摸应用程序中的按钮没有正确返回我的位置
Posted
技术标签:
【中文标题】我在可可触摸应用程序中的按钮没有正确返回我的位置【英文标题】:My button in cocoa touch app is not returning my position correctly 【发布时间】:2011-10-16 23:35:47 【问题描述】:我有一个应用程序,其按钮覆盖整个屏幕,并且在此按钮顶部有较小的按钮。当我按下按钮时,我写了一个显示手指位置的文本,但是当我将手指放在某些区域时,我看不到任何东西,按钮停止工作(屏幕的右下方区域)我确定在那里那里什么都没有。代码如下:
.h 文件:
UIButton *BigButton;
CGPoint FingerPos;
bool isFingerDown;
UILabel *FingerPosLabel;
-(void)Update;
-(IBAction)FingerMoved:(id)sender withEvent:(UIEvent*)event;
-(IBAction)FingerPressed:(id)sender withEvent:(UIEvent*)event;
-(IBAction)FingerReleased:(id)sender withEvent:(UIEvent*)event;
.m 文件
viewDidLoad
BigButton = [UIButton buttonWithType:UIButtonTypeCustom];
[BigButton setFrame:CGRectMake(0, 0, 300, 480)];
[self.view addSubview:BigButton];
FingerPos = CGPointMake(0, 0);
isFingerDown = false;
FingerPosLabel = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 300, 50)];
[self.view addSubview:FingerPosLabel];
[FingerPosLabel setBackgroundColor:[UIColor colorWithRed:0 green:0 blue:0 alpha:1]];
[FingerPosLabel setTextColor:[UIColor redColor]];
[BigButton addTarget:self action:@selector(FingerPressed:withEvent:) forControlEvents:UIControlEventTouchDown];
[BigButton addTarget:self action:@selector(FingerReleased:withEvent:) forControlEvents:UIControlEventTouchUpInside];
[BigButton addTarget:self action:@selector(FingerMoved:withEvent:) forControlEvents:UIControlEventTouchDragInside];
-(IBAction)FingerPressed:(id)sender withEvent:(UIEvent *)event
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint endingPosition = [touch locationInView:self.view];
FingerPos = endingPosition;
isFingerDown = true;
-(IBAction)FingerMoved:(id)sender withEvent:(UIEvent *)event
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint endingPosition = [touch locationInView:self.view];
FingerPos = endingPosition;
-(IBAction)FingerReleased:(id)sender withEvent:(UIEvent *)event
NSSet *touches = [event allTouches];
UITouch *touch = [touches anyObject];
CGPoint endingPosition = [touch locationInView:self.view];
FingerPos = endingPosition;
isFingerDown = false;
我确定按钮 BigButton 上方没有任何按钮或任何类型的视图,尽管在不可见区域上方有一个按钮,虽然很远
【问题讨论】:
【参考方案1】:问题解决了。屏幕是横向的,按钮不够宽。我倒了宽高(应该是480、300)
【讨论】:
它不会帮助你解决这个问题,但将来你可以通过在模拟器中打开“调试->颜色混合层”来调试丢失的触摸。就在昨天,它帮助我调试了一个与我的按钮重叠并吞下触摸事件的透明视图。以上是关于我在可可触摸应用程序中的按钮没有正确返回我的位置的主要内容,如果未能解决你的问题,请参考以下文章