Reputation: 1510
What I want to achieve: I want to create a copy of selected view.
I have two UIImageViews
on UIView
. I am selecting both the views and want to clone both the views.
Everything is working fine until I am not rotating the UIImageView
. If am rotating the view UIImageView
changing its 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];
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];
}
Here is a screenshot of how it looks like now:
Upvotes: 0
Views: 102
Reputation: 12144
After rotating the image, its frame won't work anymore and that's the reason for your issue.
You can take a look at Apple document for UIView's transform property
When the value of this property is anything other than the identity transform, the value in the frame property is undefined and should be ignored.
The solution for this situation is using center
and transform
properties instead of 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];
}
Upvotes: 2