放大UIImageView和UIImageView.center

时间:2012-10-16 16:09:23

标签: ios cocoa-touch uiview uiimageview

尝试放大/缩小imageView。

我已经成功管理了所以现在我正在尝试在视图上放置一些按钮,当按钮点击以放大该区域时。

我该如何实现?

这是我到目前为止的代码

-(void)zoomHere:(UIButton *)sender{

    NSLog(@"zoom in");
    //[self performSelector:@selector()
    //         withObject:nil
    //         afterDelay:1.6f];


    CGAffineTransform transformer = CGAffineTransformScale(imageView.transform, 1.05, 1.05);



    if (transformer.a < 5) {
        [UIView beginAnimations:nil context:NULL];
        [UIView setAnimationDuration: 0.2];
        imageView.transform = transformer;
        [UIView setAnimationDelegate:self];
        [UIView commitAnimations];
    }


}

并且视图已加载

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIView *buttonView = [[UIView alloc]initWithFrame:CGRectMake(00.0, 20.0, 768, 1004.0)];

    zoomHere = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    [zoomHere addTarget:self
               action:@selector(zoomHere:)
     forControlEvents:UIControlEventTouchDown];
    [zoomHere setTitle:@"ZoomIN" forState:UIControlStateNormal];
    zoomHere.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
    [buttonView addSubview:zoomHere];

    buttonView.multipleTouchEnabled = YES;

    [self.view addSubview:buttonView];


}

它仍然可以放大,但不会放在按钮位于屏幕上的区域!

1 个答案:

答案 0 :(得分:3)

如果你想使用缩放变换,你可能也应该使用translate变换,如下所示:

CGAffineTransform scale = CGAffineTransformMakeScale(1.05, 1.05);
CGAffineTransform translate = CGAffineTransformMakeTranslation(10, 20);
CGAffineTransform delta = CGAffineTransformConcat(scale, translate);
imageView.transform = CGAffineTransformConcat(imageView.transform, delta);

这就是你知道图像视图上按钮位置的方式:

[imageView convertPoint:button.center fromView:self.view]

所以,如果您知道按钮的坐标和当前比例,那么您可以计算新的平移变换。但是对于我来说,我宁愿使用滚动视图来放大和缩小该图像视图。你可以有漂亮的弹跳动画和缩放动画,也可以有平移和捏,但是cource它取​​决于你。