使用 CGAffineTransform 時限制 UIImageView 的大小 (Limitting UIImageView size while using CGAffineTransform)


問題描述

使用 CGAffineTransform 時限制 UIImageView 的大小 (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];
}

參考解法

方法 1:

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

方法 2:

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

(by user1560010Paresh NavadiyaAhmed Elashker)

參考文件

  1. Limitting UIImageView size while using CGAffineTransform (CC BY-SA 3.0/4.0)

#cgaffinetransform #iphone #scale #iOS #uiimageview






相關問題

CGAffineTransform:將比例應用於翻譯,如何? (CGAffineTransform: apply a Scale to a Translation, how?)

Facebook不尊重AVMutableCompositionTrack上的preferredTransform標誌 (Facebook not Respecting preferredTransform flag on AVMutableCompositionTrack)

使用 CGAffineTransform 時限制 UIImageView 的大小 (Limitting UIImageView size while using CGAffineTransform)

Выгляд павернутай табліцы не бачны ў iOS 6.0 (Rotated Table View is not visible in iOS 6.0)

使用 CGAffineTransformRotate,如何將旋轉設置為 0 (Using CGAffineTransformRotate, how to set rotation to 0)

Hành vi CGAffineTransformRotate không nhất quán giữa chế độ xem và bộ điều khiển chế độ xem (CGAffineTransformRotate behaviour inconsistent between view and view controller)

將 UIView.transform 設置為任意翻譯 CGAffineTransform 什麼都不做 (Setting UIView.transform to arbitrary translate CGAffineTransform does nothing)

使用 CGAfflineTransformMakeScale/Rotation 只做一個動作 (Using CGAfflineTransformMakeScale/Rotation only does one action)

使用 CGAffine 旋轉視圖後的平移 (translation after rotation view using CGAffine)

NSViews 上的 CGAffineTransforms (CGAffineTransforms on NSViews)

旋轉後向一個方向縮放 UIView 時,如何防止傾斜? (How can I prevent skew when scaling a UIView in one direction after rotation?)

使用捏合手勢縮放 UIImageView 的問題 (Problem scaling UIImageView with Pinch Gesture)







留言討論