拡大された画像の値がおかしい
拡大表示されている画像の値がおかしいです。
画像の拡大にはCABasicAnimation
を使用しています。(詳細は下記にコードを記しています)
拡大後の画像で色々とピンチインさせたりボタンタップのイベントを拾ったりしようとしたのですが、何も反応してくれません。試しに画像の大きさのログを取ってみると、拡大前と拡大後で同じ値となっていました。これはどういうことなのでしょうか?正しく拡大できていないのでしょうか?
実際ImageView
にScrollView
を乗せて、ScrollView
の背景に色を付けて動作させたところ、ImageView
が拡大した後にImageView
が緑色で覆われました。ということは拡大はできているのでしょうか?しかし値はおかしなままです。どなたか分かる方がいれば教えていただきたいです。すみませんが、よろしくお願いします。
ViewController.h
@interface ViewController : UIViewController<UIScrollViewDelegate>
@property (weak, nonatomic) IBOutlet UIImageView *imageButtonView;
@property (weak, nonatomic) IBOutlet UIImageView *cancelButton;
@end
ViewController.m
@interface ViewController () {
UIScrollView *scrollView;
}
@end
@implementation ViewController
bool isImageMax = false;
- (void)viewDidLoad {
[super viewDidLoad];
_imageButtonView.tag = 100;
_imageButtonView.userInteractionEnabled = YES;
_imageButtonView.multipleTouchEnabled = YES;
_imageButtonView.image = [UIImage imageNamed:@"coupon.png"];
NSLog(@"%f: %f", _imageButtonView.bounds.size.width, _imageButtonView.bounds.size.height);
_cancelButton.tag = 200;
_cancelButton.userInteractionEnabled = YES;
_cancelButton.hidden = YES;
scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
scrollView.delegate = self;
scrollView.minimumZoomScale = 1.0;
scrollView.maximumZoomScale = 2.0;
[_imageButtonView addSubview:scrollView];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
UITouch *touch = [[event allTouches] anyObject];
if (touch.view.tag == _imageButtonView.tag) {
self.view.backgroundColor = [UIColor blackColor];
_imageButtonView.userInteractionEnabled = NO;
[self imageMoveAndScale:1.0 scaleTo:2.0 moveFrom:_imageButtonView.layer.position moveTo:CGPointMake(self.view.center.x, self.view.center.y) duration:0.5];
} else if (touch.view.tag == _cancelButton.tag) {
[self imageMoveAndScale:2.0 scaleTo:1.0 moveFrom:CGPointMake(self.view.center.x, self.view.center.y) moveTo:_imageButtonView.layer.position duration:0];
}
}
- (void)imageMoveAndScale:(float)scaleFrom scaleTo:(float)scaleTo moveFrom:(CGPoint)moveFrom moveTo:(CGPoint)moveTo duration:(float)duration {
CABasicAnimation *scaleAnimation = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
scaleAnimation.fromValue = [NSNumber numberWithFloat:scaleFrom];
scaleAnimation.toValue = [NSNumber numberWithFloat:scaleTo];
CABasicAnimation *moveAnimation = [CABasicAnimation animationWithKeyPath:@"position"];
moveAnimation.duration = duration;
moveAnimation.repeatCount = 1;
moveAnimation.fromValue = [NSValue valueWithCGPoint:moveFrom];
moveAnimation.toValue = [NSValue valueWithCGPoint:moveTo];
CAAnimationGroup *group = [CAAnimationGroup animation];
group.duration = duration;
group.repeatCount = 1;
group.delegate = self;
group.removedOnCompletion = NO;
group.fillMode = kCAFillModeForwards;
group.animations = [NSArray arrayWithObjects:scaleAnimation, moveAnimation, nil];
[_imageButtonView.layer addAnimation:group forKey:@"move-scale-layer"];
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
return _imageButtonView;
}
- (CGFloat)distanceWithPointA:(CGPoint)pointA pointB:(CGPoint)pointB
{
CGFloat dx = fabs( pointB.x - pointA.x );
CGFloat dy = fabs( pointB.y - pointA.y );
return sqrt(dx * dx + dy * dy);
}
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag {
if (isImageMax) {
self.view.backgroundColor = [UIColor whiteColor];
_cancelButton.hidden = YES;
_imageButtonView.userInteractionEnabled = YES;
isImageMax = false;
scrollView.frame = CGRectMake(0, 0, 0, 0);
} else {
_cancelButton.hidden = NO;
isImageMax = true;
scrollView.frame = CGRectMake(_imageButtonView.center.x, _imageButtonView.center.y, _imageButtonView.bounds.size.width, _imageButtonView.bounds.size.height);
}
}
@end