Graphics Reference
In-Depth Information
Listing 3.4
Determining the Touched Layer Using
containsPoint:
@interface
ViewController ()
@property
(
nonatomic
,
weak
)
IBOutlet
UIView
*layerView;
@property
(
nonatomic
,
weak
)
CALayer
*blueLayer;
@end
@implementation
ViewController
- (
void
)viewDidLoad
{
[
super
viewDidLoad
];
//create sublayer
self
.blueLayer = [
CALayer
layer
];
self
.blueLayer.
frame
=
CGRectMake
(
50.0f
,
50.0f
,
100.0f
,
100.0f
);
self
.blueLayer.
backgroundColor
= [
UIColor
blueColor
].
CGColor
;
//add it to our view
[
self
.layerView.
layer
addSublayer
:
self
.blueLayer];
}
- (
void
)touchesBegan:(
NSSet
*)touches withEvent:(
UIEvent
*)event
{
//get touch position relative to main view
CGPoint
point = [[touches
anyObject
]
locationInView
:
self
.
view
];
//convert point to the white layer's coordinates
point = [
self
.layerView.
layer
convertPoint
:point
fromLayer
:
self
.view.
layer
];
//get layer using containsPoint:
if
([
self
.layerView.
layer
containsPoint
:point])
{
//convert point to blueLayer's coordinates
point = [
self
.blueLayer
convertPoint
:point
fromLayer
:
self
.layerView.
layer
];
if
([
self
.blueLayer
containsPoint
:point])
{
[[[
UIAlertView
alloc
]
initWithTitle
:
@"Inside Blue Layer"
message
:
nil
delegate
:
nil
cancelButtonTitle
:
@"OK"