Detect a two-finger pinch or spread (or neither).
Since we are using a
UIPinchGestureRecognizer
(a subclass of
UIGestureRecognizer
),
we do not need to turn on the big white view’s
multipleTouchEnabled
property or the yellow label’s
userInteractionEnabled
property.
To simulate two fingers, press the option key as you drag on the simulator. To keep the distance between the fingers constant, hold down the shift key too.
main.m
PinchAppDelegate
View
assad.jpg
to png and add it to the Supporting Files folder of the project.
Change the yellow
UILabel
to a
UIImageView
.
An
UIImageView
is a
UIView
that is specialized for displaying one image,
just as a
UILabel
is a
UIView
that is specialized for displaying one line of text.
The image displayed in a
UIImageView
is a
UIImage
(see
America),
and the line of text displayed in a
UILabel
is an
NSString
.
In
View.h
,
change the
UILabel
instance variable to the following.
UIImageView *imageView;In
View.m
,
change the initialization of the label in
initWithFrame:
to the following.
UIImage *image = [UIImage imageNamed: @"assad.jpg"]; if (image == nil) { NSLog(@"could not create image"); return nil; } imageView = [[UIImageView alloc] initWithImage: image]; //Keep the UIImageView the same size, //but center it in the View. CGRect b = self.bounds; imageView.center = CGPointMake( b.origin.x + b.size.width / 2, b.origin.y + b.size.height/ 2 ); [self addSubview: imageView];In the
pinch:
method,
resize the
ImageView
.
The
ImageView
’s
is overridden by the value we gave to the
center
property of the
ImageView
.
The center of the
ImageView
remains at the center of the big white
View
.
CGSize i = imageView.image.size; imageView.bounds = CGRectMake(0, 0, scale * i.width, scale * i.height);