gpt4 book ai didi

iphone - 使用 CGAffineTransform 时限制 UIImageView 的大小

转载 作者:行者123 更新时间:2023-11-28 20:29:41 24 4
gpt4 key购买 nike

我想在使用 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];
}

最佳答案

应用转换之前,您可以知道 UIImagViewCGRect,如下例所示:

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

关于iphone - 使用 CGAffineTransform 时限制 UIImageView 的大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12704444/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com