使用一个手指旋转UIView的代码

本文提供了一个使用Swift实现的手势识别代码片段,用于根据单指触摸旋转视图。通过捕获触摸事件并计算触摸点的变化,实现平滑的旋转效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Code Snippet for rotating a view with single finger.

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    UITouch *touch = [touches anyObject];
    touch1=[touch locationInView:self.superview];
    touch2=[touch previousLocationInView:self.superview];

    
    
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
{
    
    UITouch *touch = [touches anyObject];
    CGPoint currentTouch1=[touch locationInView:self.superview];
    CGPoint currentTouch2=[touch previousLocationInView:self.superview];

    
    CGFloat previousAngle = atan2(touch2.y - touch1.ytouch2.x - touch1.x) * 180 / 3.14;
    CGFloat currentAngle = atan2(currentTouch2.y - currentTouch1.y,currentTouch2.x - currentTouch1.x) * 180 / 3.14;
    
    CGFloat angleToRotate = currentAngle - previousAngle;
    
    CGAffineTransform transform = CGAffineTransformRotate(self.transform, angleToRotate*3.14/180);
    
    self.transform = transform;
    touch1 = currentTouch1;
    touch2 = currentTouch2;
    
    
}

-(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值