swiftui中使用scaleEffect和rotationEffect实现缩放和旋转效果

在SwiftUI中,你可以使用.scaleEffect()和.rotationEffect()来实现缩放和旋转动画,缩放和旋转的内容可以是图片,文字等view视图。

scaleEffect可以实现缩放效果,配合动画可以实现好看的过度效果,其中的参数是缩放的倍数,1表示原本大小,大于1表示放大,小于1表示缩小。

示例代码:

Text("Hello, World!")
                .font(.title)
                .scaleEffect(useEffect ? 1 : 0.7)

rotationEffect表示实现旋转效果,配合动画可以实现好看的旋转效果,其中的参数是旋转的度数,示例代码:

Image("xigua")
                .resizable()
                .frame(width: 200, height: 200)
                .scaleEffect(useEffect ? 1 : 0.7)
                .rotationEffect(.degrees(useEffect ? 100 : 0))

案例使用代码:

//
//  ScalRotaEffect.swift
//  SwiftBook
//
//  Created by Song on 2024/7/21.
//

import SwiftUI

struct ScalRotaEffect: View {
    // 缩放和旋转效果
    @State var useEffect = false
    var body: some View {
        VStack {
            Text("Hello, World!")
                .font(.title)
                .scaleEffect(useEffect ? 1 : 0.7)
            Image("xigua")
                .resizable()
                .frame(width: 200, height: 200)
                .scaleEffect(useEffect ? 1 : 0.7)
                .rotationEffect(.degrees(useEffect ? 100 : 0))
            Button(action: {
                withAnimation { useEffect.toggle() }
            }, label: {
                Text("旋转和缩放")
            }).padding()
        }
    }
}

#Preview {
    ScalRotaEffect()
}

 

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是一个简单的示例代码,可以让相机围绕物体进行旋转: ```csharp using UnityEngine; public class ObjectRotate : MonoBehaviour { public Transform target; // 目标物体 public float rotateSpeed = 5f; // 旋转速度 public float zoomSpeed = 2f; // 速度 public float minDistance = 1f; // 最小距离 public float maxDistance = 10f; // 最大距离 private Vector3 offset; // 初始偏移量 private float distance; // 相机与目标物体的距离 void Start() { offset = transform.position - target.position; distance = offset.magnitude; } void LateUpdate() { // float scroll = Input.GetAxis("Mouse ScrollWheel"); distance -= scroll * zoomSpeed; distance = Mathf.Clamp(distance, minDistance, maxDistance); // 旋转 if (Input.GetMouseButton(0)) { float horizontal = Input.GetAxis("Mouse X") * rotateSpeed; float vertical = Input.GetAxis("Mouse Y") * rotateSpeed; transform.RotateAround(target.position, Vector3.up, horizontal); transform.RotateAround(target.position, transform.right, -vertical); } // 更新相机位置 transform.position = target.position + offset.normalized * distance; } } ``` 在脚本,我们定义了目标物体、旋转速度、最小和最大距离等参数,并在 `Start()` 方法计算了初始偏移量和距离。在 `LateUpdate()` 方法,我们根据鼠标滚轮相机距离,根据鼠标左键旋转相机,并根据距离更新相机位置。 请注意,这只是一个简单的示例代码,你可以根据需要进行修改和优化。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1024小神

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值