When my UIView
使用变换属性旋转(CGAffineTransformMakeRotation
),我需要拖动它的一个角(例如右下角)来调整它的大小。在此过程中,当用户拖动角时,视图的角必须跟随用户的手指,并通过增加 2 个边(右下角拖动的右侧和底部尺寸)来调整框的大小。
调整大小并不那么困难UIView
当盒子未变形时,您可以使用框架和触摸位置。例如,我会记住初始帧并触摸UIPanGestureRecognizer
handler
附加到这个可调整大小的视图状态开始,以及关于状态改变我会计算触摸点 X、Y 与初始触摸的差异,并将这些值添加到初始框架宽度和高度。
然而,框架对于旋转是可靠的UIView
当应用变换时。于是我只能依靠bounds
and center
。我创建了这段代码,但它几乎可以工作:我只能在所有 4 个方向上按比例放大视图,而不是一个方向。
- (void)handleResizeGesture:(UIPanGestureRecognizer *)recognizer {
CGPoint touchLocation = [recognizer locationInView:self.superview];
CGPoint center = self.center;
switch (recognizer.state) {
case UIGestureRecognizerStateBegan: {
deltaAngle = atan2f(touchLocation.y - center.y, touchLocation.x - center.x) - CGAffineTransformGetAngle(self.transform);
initialBounds = self.bounds;
initialDistance = CGPointGetDistance(center, touchLocation);
initialLocation = touchLocation;
if ([self.delegate respondsToSelector:@selector(stickerViewDidBeginRotating:)]) {
[self.delegate stickerViewDidBeginRotating:self];
}
break;
}
case UIGestureRecognizerStateChanged: {
CGFloat scale = CGPointGetDistance(center, touchLocation)/initialDistance;
CGFloat minimumScale = self.minimumSize/MIN(initialBounds.size.width, initialBounds.size.height);
scale = MAX(scale, minimumScale);
CGRect scaledBounds = CGRectScale(initialBounds, scale, scale);
self.bounds = scaledBounds;
[self setNeedsDisplay];
if ([self.delegate respondsToSelector:@selector(stickerViewDidChangeRotating:)]) {
[self.delegate stickerViewDidChangeRotating:self];
}
break;
}
case UIGestureRecognizerStateEnded:
if ([self.delegate respondsToSelector:@selector(stickerViewDidEndRotating:)]) {
[self.delegate stickerViewDidEndRotating:self];
}
break;
default:
break;
}
}
下图显示了已知值(A、B、x、y、N、M、N-M 距离)的旋转视图:
好问题。我刚刚创建了一个使用类似内容的类。在我的课程中,您可以更改 UIViewA 的比例,它将更改 UIViewB 的比例,同时保持相同的比例。
检查左上角的白色小方块:
该类可以在这里找到:https://github.com/sSegev/SSUIViewMiniMe https://github.com/sSegev/SSUIViewMiniMe
你问的有点不同。我正在更改正方形的总大小,而您只想根据用户拖动它的距离来更改相对于其中一个角的大小。
因为你不能在动画播放时点击按钮(嗯,你可以,但动画只是养眼,视图已经处于最终状态),我使用了 CABasicAnimation,这使它更容易。
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
//Rotate the UIView
if ([_myRedView.layer animationForKey:@"SpinAnimation"] == nil)
{
CABasicAnimation* animation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
animation.fromValue = [NSNumber numberWithFloat:0.0f];
animation.toValue = [NSNumber numberWithFloat: 2*M_PI];
animation.duration = 50.0f;
animation.repeatCount = INFINITY;
[self.myRedView.layer addAnimation:animation forKey:@"SpinAnimation"];
}
}
// That's the main chunk of code:
- (void)dragBegan:(UIControl *)c withEvent:ev
{
UITouch *touch = [[ev allTouches] anyObject];
CGPoint touchPoint = [touch locationInView:_myRedView];
NSLog(@"Touch x : %f y : %f", touchPoint.x, touchPoint.y);
if(_myRedView.width/2<touchPoint.x && _myRedView.height/2<touchPoint.y) //Checks for right bottom corner
{
_myRedView.width =touchPoint.x;
_myRedView.height = touchPoint.y;
dragButton.frame = CGRectMake(0, 0, _myRedView.width, _myRedView.height);
}
}
我确信需要进行一些调整,但现在看起来像这样:
*
只需 6 行代码就很酷了哈哈?
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)