旋转手势改变视图框架
Posted
技术标签:
【中文标题】旋转手势改变视图框架【英文标题】:Rotation gesture changing view frame 【发布时间】:2019-09-27 09:03:42 【问题描述】:我想要实现的目标:我想要创建所选视图的副本。
我在UIView
上有两个UIImageViews
。我正在选择这两个视图并希望克隆这两个视图。
在我不旋转 UIImageView
之前一切正常。如果正在旋转视图UIImageView
更改其框架。
UIView *viewClone = [[UIView alloc]initWithFrame:CGRectMake(fltLeadingMin, fltTopMin, fltCloneViewWidth, fltCloneViewHeight)];
viewClone.backgroundColor = [UIColor redColor];
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];
[viewClone addGestureRecognizer:panGesture];
for (UIImageView *imgView in arrSelectedViews)
NSData *archivedData = [NSKeyedArchiver archivedDataWithRootObject:imgView];
UIImageView *imgViewClone = [NSKeyedUnarchiver unarchiveObjectWithData:archivedData];
imgViewClone.frame = CGRectMake(imgViewClone.frame.origin.x - viewClone.frame.origin.x, imgViewClone.frame.origin.y - viewClone.frame.origin.y, imgView.frame.size.width, imgView.frame.size.height);
imgViewClone.backgroundColor = UIColor.blueColor;
[viewClone addSubview:imgViewClone];
这是它现在的截图:
【问题讨论】:
【参考方案1】:旋转图像后,它的框架将不再起作用,这就是您出现问题的原因。
你可以看看Apple document for UIView's transform property
当该属性的值不是恒等变换时,frame 属性中的值是未定义的,应该被忽略。
这种情况的解决方案是使用center
和transform
属性而不是frame
。
UIView *viewClone = [[UIView alloc]initWithFrame:CGRectMake(fltLeadingMin, fltTopMin, fltCloneViewWidth, fltCloneViewHeight)];
viewClone.backgroundColor = [UIColor redColor];
UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(handlePan:)];
[viewClone addGestureRecognizer:panGesture];
for (UIImageView *imgView in arrSelectedViews)
NSData *archivedData = [NSKeyedArchiver archivedDataWithRootObject:imgView];
UIImageView *imgViewClone = [NSKeyedUnarchiver unarchiveObjectWithData:archivedData];
// This is the frame before you rotate image, can be replace with another size
CGRect originalFrame = CGRectMake(0, 0, 200, 200);
imgViewClone.frame = originalFrame
// Using center and transform instead of current frame
imgViewClone.center = imgView.center;
imgViewClone.transform = imgView.transform;
imgViewClone.backgroundColor = UIColor.blueColor;
[viewClone addSubview:imgViewClone];
【讨论】:
以上是关于旋转手势改变视图框架的主要内容,如果未能解决你的问题,请参考以下文章