4th*_*ace 4 iphone cocoa-touch core-graphics objective-c uiimageview
如果我有一个UIImageView并想知道用户是否点击了图像.在touchesBegan中,我执行以下操作,但总是在第一个条件中结束.窗口处于纵向模式,图像位于底部.我可以点击窗口的右上角仍然进入第一个状态,这看起来非常不正确.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
UITouch *touch = [touches anyObject];
CGPoint location = [touch locationInView:touch.view];
if(CGRectContainsPoint(myimage.frame, location) == 0){
//always end up here
}
else
{ //user didn't tap inside image}
Run Code Online (Sandbox Code Playgroud)
和值是:
location: x=303,y=102
frame: origin=(x=210,y=394) size=(width=90, height=15)
Run Code Online (Sandbox Code Playgroud)
有什么建议?
Pet*_*wis 19
首先,你接触到:
UITouch *touch = [[event allTouches] anyObject];
Run Code Online (Sandbox Code Playgroud)
接下来,您要检查相对于图像视图的locationInView.
CGPoint location = [touch locationInView:self]; // or possibly myimage instead of self.
Run Code Online (Sandbox Code Playgroud)
接下来,CGRectContainsPoint返回一个布尔值,因此将其与0进行比较非常奇怪.它应该是:
if ( CGRectContainsPoint( myimage.frame, location ) ) {
// inside
} else {
// outside
}
Run Code Online (Sandbox Code Playgroud)
但是如果self不是myimage那么myimage视图可能是触摸而不是你 - 从你的问题不清楚什么是对象self它不是UIImageView的子类.
| 归档时间: |
|
| 查看次数: |
17860 次 |
| 最近记录: |