Iphone 使用CGAffineTransform时限制UIImageView大小
我想在使用CGAffineTransform时限制uiimageview的大小。我正在限制缩放,但无法限制uiimageview的大小。当我运行应用程序时,我看到它受到限制,但在后台,我的uiimageview的大小不断增加。我怎么做这个 这是我的密码:Iphone 使用CGAffineTransform时限制UIImageView大小,iphone,ios,uiimageview,scale,cgaffinetransform,Iphone,Ios,Uiimageview,Scale,Cgaffinetransform,我想在使用CGAffineTransform时限制uiimageview的大小。我正在限制缩放,但无法限制uiimageview的大小。当我运行应用程序时,我看到它受到限制,但在后台,我的uiimageview的大小不断增加。我怎么做这个 这是我的密码: - (id)initWithFrame:(CGRect)frame { if ([super initWithFrame:frame] == nil) { return nil; } originalT
- (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];
}
-(id)initWithFrame:(CGRect)frame
{
if([super initWithFrame:frame]==nil){
返回零;
}
原始转换=CGAffinetransformity;
touchBeginPoints=CFDictionaryCreateMutable(NULL,0,NULL,NULL);
self.userInteractionEnabled=是;
self.multipleTouchEnabled=是;
self.exclusiveTouch=是;
回归自我;
}
-(无效)触摸开始:(NSSet*)触摸事件:(UIEvent*)事件{
NSMutableSet*CurrentTouchs=[[[event touchesForView:self]mutableCopy]autorelease];
[CurrentTouchs minusSet:Touchs];
如果([CurrentTouchs count]>0){
[自更新原始TransformForTouches:CurrentTouchs];
[自缓存BeginPointforTouchs:CurrentTouchs];
}
[超级触摸开始:触摸事件:事件];
[自缓存BeginPointforTouchs:Touchs];
}
-(无效)触摸移动:(NSSet*)触摸事件:(UIEvent*)事件{
CGAffineTransform incrementalTransform=[self-IncrementalTransformWithTouchs:[event touchesForView:self]];
self.transform=CGAffineTransformConcat(原始转换、增量转换);
CG仿射变换=自变换;
浮动比例=sqrt(transform.a*transform.a+transform.c*transform.c);
NSLog(@“%f”,自帧大小高度);
如果(缩放>缩放最大值){
self.transform=CGAffineTransformScale(变换,缩放最大/缩放,缩放最大/缩放);
}
否则如果(刻度<刻度最小值){
self.transform=CGAffineTransformScale(变换、缩放最小/缩放、缩放最小/缩放);
}
[超级触摸移动:触摸事件:事件];
}
-(void)updateOriginalTransformForTouches:(NSSet*)触摸{
CG仿射变换=自变换;
浮动比例=sqrt(transform.a*transform.a+transform.c*transform.c);
如果(缩放>缩放最大值){
self.transform=CGAffineTransformScale(变换,缩放最大/缩放,缩放最大/缩放);
}
否则如果(刻度<刻度最小值){
self.transform=CGAffineTransformScale(变换、缩放最小/缩放、缩放最小/缩放);
}
}
-(void)touchesend:(NSSet*)toucheevent:(UIEvent*)event{
用于(UITouch*触摸屏){
如果(touch.tapCount>=2){
[self.superview将子视图带到前面:self];
}
}
[self-updateOriginalTransformForTouches:[event touchesForView:self]];
[self-removeTouchesFromCache:Touchs];
NSMutableSet*remainingtouchs=[[event touchesForView:self]mutableCopy]autorelease];
[RemainingTouchs minusSet:Touchs];
[超级触控:触控事件:事件];
[自缓存BeginPointforTouchs:RemainingTouchs];
}
-(无效)触控取消:(NSSet*)触控事件:(UIEvent*)事件{
[self-touchesedend:toucheevent:event];
}
在对其应用transform之前,您可以知道UIImagView的CGRect
,如下示例:
//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))
}
在UIImagView
上应用transform之前,您可以知道CGRect
,如下示例:
//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))
}
我面临着一个类似的问题,我有一个图像视图,它根据罗盘航向数据旋转,还必须根据接口方向的设备变化移动位置
- (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];
}
我创建了一个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];
}
我面临着一个类似的问题,我有一个图像视图,它根据罗盘航向数据旋转,还必须根据接口方向的设备变化移动位置
- (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];
}
我创建了一个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];
}