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 に答える