0

我想在使用 CGAffineTransform 时限制我的 uiimageview 大小。我正在限制规模,但我不能限制我的 uiimageview 的大小。当我运行该应用程序时,我看到它是有限的,但在后台我的 uiimageview 的大小不断增加。我怎样才能做到这一点?

这是我的代码:

- (id)initWithFrame:(CGRect)frame
{
    if ([super initWithFrame:frame] == nil) {
        return nil;
    }

    originalTransform = CGAffineTransformIdentity;
    touchBeginPoints = CFDictionaryCreateMutable(NULL, 0, NULL, NULL);
    self.userInteractionEnabled = YES;
    self.multipleTouchEnabled = YES;
    self.exclusiveTouch = YES;

    return self;
}


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
        NSMutableSet *currentTouches = [[[event touchesForView:self] mutableCopy] autorelease];
        [currentTouches minusSet:touches];
        if ([currentTouches count] > 0) {
            [self updateOriginalTransformForTouches:currentTouches];
            [self cacheBeginPointForTouches:currentTouches];
        }
        [super touchesBegan:touches withEvent:event];
        [self cacheBeginPointForTouches:touches];

}



- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {

    CGAffineTransform incrementalTransform = [self incrementalTransformWithTouches:[event touchesForView:self]];
    self.transform = CGAffineTransformConcat(originalTransform, incrementalTransform);

    CGAffineTransform transform = self.transform;
    float scale = sqrt(transform.a*transform.a + transform.c*transform.c);
    NSLog(@"%f",self.frame.size.height);

    if (scale > SCALE_MAX){
        self.transform = CGAffineTransformScale(transform, SCALE_MAX/scale, SCALE_MAX/scale);
    }
    else if (scale < SCALE_MIN){
        self.transform = CGAffineTransformScale(transform, SCALE_MIN/scale, SCALE_MIN/scale);
    }
    [super touchesMoved:touches withEvent:event];
}

-(void)updateOriginalTransformForTouches:(NSSet *)touches{
    CGAffineTransform transform = self.transform;
    float scale = sqrt(transform.a*transform.a + transform.c*transform.c);

    if (scale > SCALE_MAX){
        self.transform = CGAffineTransformScale(transform, SCALE_MAX/scale, SCALE_MAX/scale);
    }
    else if (scale < SCALE_MIN){
        self.transform = CGAffineTransformScale(transform, SCALE_MIN/scale, SCALE_MIN/scale);
    }

}

- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
        for (UITouch *touch in touches) {
            if (touch.tapCount >= 2) {
                [self.superview bringSubviewToFront:self];
            }
        }

        [self updateOriginalTransformForTouches:[event touchesForView:self]];
        [self removeTouchesFromCache:touches];

        NSMutableSet *remainingTouches = [[[event touchesForView:self] mutableCopy] autorelease];
        [remainingTouches minusSet:touches];

        [super touchesEnded:touches withEvent:event];
        [self cacheBeginPointForTouches:remainingTouches];

}



- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event {
    [self touchesEnded:touches withEvent:event];
}
4

2 回答 2

3

您可以在对其应用转换之前知道CGRect,如下例所示:UIImagView

//Create transform
CGAffineTransform scaleTransform = CGAffineTransformMakeScale(1.2, 1.2);
//Apply transform on UIImageView to get CGRect
CRect  newRect = CGRectApplyAffineTransform(yourImgView.frame, scaleTransform);
//Check if rect is in valid limitation
if(newRect.size.height > 200 && newRect.size.width >200) //your condition to limit UIImageView's Size
{ 
   //Valid so apply transform
   yourImageView.transform = scaleTransform
} 
else
{
   //rect increases so no transform
   // no transform to UIImageView
   NSLog(@"%@",NSStringFromCGRect(yourImageView.frame))
}
于 2012-10-03T08:28:14.320 回答
0

我遇到了类似的问题,我有一个根据指南针航向数据旋转的图像视图,并且还必须根据界面方向的设备变化移动位置

我创建了一个 tempImageView 来帮助维护框架,并随着设备方向的每次变化而不断重置它

- (void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
    // equal tempImageView to imageView
    _tempImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.view.frame.size.width-75, 150, 64, 61)];
    [_tempImageView setImage:[UIImage imageNamed:@"image.gif"]];
    _tempImageView.transform = _imageView.transform;

    // reset imageView
    [_imageView removeFromSuperview];
    _imageView = _tempImageView;
    [self.view insertSubview:_imageView atIndex:1];
}
于 2015-02-02T12:20:33.103 回答