UIImageView上的CGAffineTransformRotate从Center加载

时间:2014-02-26 03:26:14

标签: ios objective-c uiimageview cgaffinetransform

应用程序处于横向模式,我有一个UIButton,点击将UIImageView附加到AppDelegate的窗口(出于'x'的原因)。我尝试应用如下代码中所示的转换,但UIImageView不会附加到右上角,而是靠近视图的中心。

-(IBAction)showFullScreenImage:(id)sender {
    if(_fullImageView==nil) {
        _fullImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0,0,[UIScreen mainScreen].bounds.size.height,[UIScreen mainScreen].bounds.size.width)];
    }
    [self.fullImageView setImage:self.fullImage];
    UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageWasTapped:)];
    self.fullImageView.userInteractionEnabled = YES;
    AppDelegate * appDelegate = (AppDelegate * ) [UIApplication sharedApplication].delegate;
    [appDelegate.window addSubview:self.fullImageView];
    CGAffineTransform t = CGAffineTransformMakeTranslation(0,0);
    self.fullImageView.transform = CGAffineTransformRotate(t, -M_PI/2);
    NSLog(@"UIImageView frame:%@",NSStringFromCGRect(self.fullImageView.frame));
    [self.fullImageView addGestureRecognizer:gestureRecognizer]; 
}

-(void)imageWasTapped:(UIGestureRecognizer *)gestureRecognizer {
    if(gestureRecognizer.state==UIGestureRecognizerStateEnded) {
        [self.fullImageView removeFromSuperview];
    } 
}

日志打印出来

  

UIImageView框架:{{124,-124},{320,568}}

我在这里做错了什么?

1 个答案:

答案 0 :(得分:0)

自己修正了。

-(IBAction)showFullScreenImage:(id)sender {
    if(_fullImageView==nil) {
        _fullImageView = [[UIImageView alloc]initWithFrame:CGRectZero]; //init with zero
    }
    [self.fullImageView setImage:self.fullImage];
    UITapGestureRecognizer *gestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imageWasTapped:)];
    self.fullImageView.userInteractionEnabled = YES;
    AppDelegate * appDelegate = (AppDelegate * ) [UIApplication sharedApplication].delegate;
    [appDelegate.window addSubview:self.fullImageView];
    self.fullImageView.transform = CGAffineTransformRotate(CGAffineTransformIdentity, -M_PI/2);
    self.fullImageView.frame = [UIScreen mainScreen].bounds; //set it here
    [self.fullImageView addGestureRecognizer:gestureRecognizer];
}