ios控制ios_iOS ShakeGesture

ios控制ios

Create shake gesture in iOS based on UIResponder’s motion events

根据UIResponder的动作事件在iOS中创建摇动手势

In general, all menu items can be listed under the blanket menu option like menu/more… which has all the options enlisted. Despite of that the less priority detail w.r.t the users like beta/alpha, app/bundle version, release date and in some case report a bug, feedback options which cannot be accommodated under menu/more screen.

通常,所有菜单项都可以在总括菜单选项下列出,例如menu / more…,其中列出了所有选项。 尽管用户的优先级较低,例如beta / alpha,应用程序/捆绑版本,发布日期,并且在某些情况下报告错误,但反馈菜单中无法容纳的反馈选项/更多屏幕。

Or in other hand, If you’re having a B2B application where the business user want to record a particular activity from any random screens in your app. at the same time you don’t want to make this feature obvious to everyone by putting a CTA on navigationbar or elsewhere in the app.

或者换句话说,如果您正在使用B2B应用程序,而业务用户希望从应用程序中的任何随机屏幕记录特定的活动。 同时,您不想通过将CTA放在导航栏或应用程序的其他位置上来使此功能对所有人显而易见。

In that situation you can listen to the motion event of UIResponder, these events can be brought up regardless screens at the same time it’s not available for users those who not intents

在这种情况下,您可以收听UIResponder的运动事件,这些事件可以在不考虑屏幕的情况下被同时显示出来,而对于那些没有意图的用户而言则不可用

响应运动事件 (Responding to Motion Events)

To capture shake gesture events below motion capture delegate methods can be used.

为了捕获运动手势下方的抖动手势事件,可以使用委托方法。

// Tells the receiver that a motion event has begun.
func motionBegan(UIEvent.EventSubtype, with: UIEvent?)// Tells the receiver that a motion event has ended.
func motionEnded(UIEvent.EventSubtype, with: UIEvent?)// Tells the receiver that a motion event has been cancelled.
func motionCancelled(UIEvent.EventSubtype, with: UIEvent?)

In Addition, the UIResponder entity, where you want to capture the shake gesture you have to configure it to receive motion events, by overriding becomeFirstResponder function.

另外,UIResponder实体(您要捕获摇动手势)必须通过重写成为firstResponder函数将其配置为接收运动事件。

override func becomeFirstResponder() -> Bool {
return true
}

Then override UIResponder’s delegate methods motionBegan and motionEnded, motionCancelled

然后重写UIResponder的委托方法motionBegan和motionEnded,motionCancelled

override func motionBegan(_ motion: UIEvent.EventSubtype, 
with event: UIEvent?) {
print(motion)
print("shake gesture has began")}

motionBegan will get called, if shake gesture began.

如果开始摇动手势,则会开始调用motionBegan。

override func motionEnded(_ motion: UIEvent.EventSubtype, 
with event: UIEvent?) {
print(motion)
print("shake gesture has ended")}

motionEnded get called, if shake gesture ended.

如果摇动手势结束,则调用motionEnded。

override func motionCancelled(_ motion: UIEvent.EventSubtype,
with event: UIEvent?) {
print(motion)
print("shake gesture has cancelled")
}

motionCancelled get called, if motion event has been interrupted.

如果运动事件已中断,则调用motionCancelled。

The interruption can be any sort of UI event. i.e., screen rotation is an example that interrupt motion/ shake gesture event.

中断可以是任何类型的UI事件。 即,屏幕旋转是中断运动/摇动手势事件的示例。

利用CoreMotion框架 (Leverage CoreMotion Framework)

Use a CMMotionManager object to start the services that report movement detected by the device's onboard sensors

使用CMMotionManager对象启动报告设备机载传感器检测到的运动的服务

Now on ViewDidLoad call the Accelerometer data’s startAccelerometerUpdates with interval.

现在,在ViewDidLoad上,按间隔调用加速度计数据的 startAccelerometerUpdates。

var coreMotionManager: CMMotionManager!
override func viewDidLoad() {
super.viewDidLoad()
coreMotionManager = CMMotionManager()
self.getDeviceMotion(coreMotionManager : coreMotionManager)
}private func getDeviceMotion(coreMotionManager : CMMotionManager) {
if coreMotionManager.isAccelerometerAvailable {
coreMotionManager.accelerometerUpdateInterval = 1
coreMotionManager.startAccelerometerUpdates(to: .main) {
(data, error) in
guard let data = data, error == nil else {
return
}
print(data)
}
}
}

In the above example I have used Accelerometer data, indicating the instantaneous acceleration of the device in three dimensional space.

在上面的示例中,我使用了加速度计数据 ,该数据指示设备在三维空间中的瞬时加速度。

Similarly, you have other motion data as well,

同样,您还有其他运动数据,

  • Gyroscope data, indicating the instantaneous rotation around the device’s three primary axes.

    陀螺仪数据 ,指示围绕设备的三个主轴的瞬时旋转。

  • Magnetometer data, indicating the device’s orientation relative to Earth’s magnetic field.

    磁力计数据 ,指示设备相对于地球磁场的方向。

  • Device-motion data, indicating key motion-related attributes such as the device’s user-initiated acceleration, its attitude, rotation rates, orientation relative to calibrated magnetic fields, and orientation relative to gravity. This data is provided by Core Motion’s sensor fusion algorithms.

    设备运动数据 ,指示与关键运动相关的属性,例如设备的用户启动的加速度,设备的姿态,转速,相对于校准磁场的方向以及相对于重力的方向。 此数据由Core Motion的传感器融合算法提供。

翻译自: https://medium.com/@sureshkumardurairaj/ios-shakegesture-ecece76d88cf

ios控制ios

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值