简体   繁体   中英

Limitting UIImageView size while using CGAffineTransform

I want to limit my uiimageview size while using CGAffineTransform. I am limitting scale but I can't limit the size of the my uiimageview. When I run the app I see it is limitted but in the background the size of my uiimageview keeps increasing. How can I make this?

Here is my code:

- (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];
}

You can know CGRect of UIImagView before applying transform on it like below example :

//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))
}

I was facing the a similar problem, I have an image view that's rotating according to compass heading data and also has to move position according to device change in interface orientation

I created a tempImageView to help maintain frame, and kept reseting it with every change in device orientation

- (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];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM