“推动"指的是“推".匹配UIInterpolatingMotionEffect? (即,在UIInterpolatingMotionEffect上访问“物理") [英] "pushes" matching UIInterpolatingMotionEffect? (i.e. access "physics" on UIInterpolatingMotionEffect)

查看:92
本文介绍了“推动"指的是“推".匹配UIInterpolatingMotionEffect? (即,在UIInterpolatingMotionEffect上访问“物理")的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用UIInterpolatingMotionEffect扭曲iPhone,即可移动图像.

With UIInterpolatingMotionEffect, twist the iPhone, and you can have an image move.

现在:想象一下一个红色块,您将使用UICollisionBehavior和UIDynamicItemBehavior在屏幕上反弹".当用户扭曲iPhone时:我希望这些框以类似的物理感觉开始移动",以使用UIInterpolatingMotionEffect.

Now: imagine a red block you will "bounce around" on screen, using UICollisionBehavior and UIDynamicItemBehavior. When the user twists the iPhone: I want the boxes to "start moving" WITH SIMILAR PHYSICS FEEL to using UIInterpolatingMotionEffect.

http://tinypic.com/player. php?v = b67mlc%3E& s = 8#.VBVEq0uZNFx

Aside:UX解释:弹性效果(例如:iPhone上的SMS)具有与iOS中的视差图像效果相同的感觉". (通过感觉",我实际上只是指相同的速度,加速度.)这是第三个效果:像视差一样,它可以稍微移动物体",但是它们可以保持移动",并会有些弹跳. (您可以说,在某种程度上结合了弹力列表效果"和视差图像效果".)

Aside:UX explanation: the bouncy effect (example: SMS on iPhone) has the same "feel" as the parallax image effect in iOS. (By "feel" I really just mean the same speed, acceleration.) This would be a third effect: like parallax it would "move things slightly" but they would "keep moving", bouncing a little. (You could say, somewhat combining the feel of bouncy-lists-effect and and parallax-images-effect.)

现在:使用CMAccelerometerData进行我描述的操作相对容易,并使用UIPushBehaviorModeInstantaneous进行推送.但这是很多凌乱的代码.

Now: it's relatively easy to do what I describe, using CMAccelerometerData, and applying pushes using UIPushBehaviorModeInstantaneous. But it's a lot of messy code.

相比之下,UIInterpolatingMotionEffect 易于使用.

In contrast, UIInterpolatingMotionEffect is ridiculously easy to use.

从本质上讲,如何从UIInterpolatingMotionEffect中获取值(然后将其用作推送).干杯!

Essentially, how can I get the values from UIInterpolatingMotionEffect (which I will then use as pushes). Cheers!

仅显示UIInterpolatingMotionEffect的值?

它简单地问:如何轻松地从UIInterpolatingMotionEffect中获取"值?也就是说,似乎难以置信的是必须认真地将CALayer子类化,等等.

It asks simply: how can one easily "just get" the values from UIInterpolatingMotionEffect ? ie, it seems incredible one has to go to the effort of carefully subclassing CALayer, etc.

推荐答案

这是通过UIInterpolatingMotionEffect更新某些行为的有趣概念,尽管我不怀疑它是为此目的而设计的.如果您想基于加速度计信息更新行为,我个人认为CMMotionManager是用于此目的的理想选择.

It's an interesting notion of updating some behavior via the UIInterpolatingMotionEffect, though I don't suspect it's designed for that. If you want to update behaviors based upon accelerometer information, I personally would have thought that the CMMotionManager is ideal for that purpose.

所需的UX不能从视频剪辑中完全清除,但看起来视频在使东西沿手机倾斜的方向继续滑动,直到您停止倾斜手机为止.如果那是您想要的,我倾向于将CMMotionManager与UIKit Dynamics UIGravityBehavior结婚:

The desired UX isn't entirely clear from the video clip, but it looks like that video is having things continue to slide in the direction the phone is tilted until you stop tilting the phone. If that's what you want, I'd be inclined to marry CMMotionManager with a UIKit Dynamics UIGravityBehavior:

self.animator = [[UIDynamicAnimator alloc] initWithReferenceView:container];

UICollisionBehavior *collision = [[UICollisionBehavior alloc] initWithItems:container.subviews];
collision.translatesReferenceBoundsIntoBoundary = YES;
[self.animator addBehavior:collision];

UIGravityBehavior *gravity = [[UIGravityBehavior alloc] initWithItems:container.subviews];
gravity.gravityDirection = CGVectorMake(0, 0);
[self.animator addBehavior:gravity];

self.motionManager = [[CMMotionManager alloc] init];

typeof(self) __weak weakSelf = self;

[self.motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMDeviceMotion *motion, NSError *error) {
    if (weakSelf.referenceAttitude == nil) {
        weakSelf.referenceAttitude = motion.attitude;
    } else {
        CMAttitude *attitude = motion.attitude;
        [attitude multiplyByInverseOfAttitude:weakSelf.referenceAttitude];
        gravity.gravityDirection = CGVectorMake(attitude.roll * 5.0, attitude.pitch * 5.0);
    }
}];

如果希望他们精确地按照姿势移动,在停止移动设备时停止移动(如UIInterpolatingMotionEffect那样),则可以使用UIAttachmentBehavior,例如:

If you want them to move precisely in accordance to the attitude, stopping the movement when you stop moving the device (like UIInterpolatingMotionEffect does), you could use a UIAttachmentBehavior, something like:

UIAttachmentBehavior *attachment = [[UIAttachmentBehavior alloc] initWithItem:viewToAttachTo attachedToAnchor:viewToAttachTo.center];
[self.animator addBehavior:attachment];

self.motionManager = [[CMMotionManager alloc] init];
self.motionManager.deviceMotionUpdateInterval = 1.0 / 20.0;

typeof(self) __weak weakSelf = self;

CGPoint originalAnchorPoint = viewToAttachTo.center;

[self.motionManager startDeviceMotionUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMDeviceMotion *motion, NSError *error) {
    if (weakSelf.referenceAttitude == nil) {
        weakSelf.referenceAttitude = motion.attitude;
    } else {
        CMAttitude *attitude = motion.attitude;
        [attitude multiplyByInverseOfAttitude:weakSelf.referenceAttitude];
        attachment.anchorPoint = CGPointMake(originalAnchorPoint.x + attitude.roll * 10.0, originalAnchorPoint.y + attitude.pitch * 10.0);
    }
}];

请注意,在这两个示例中,当用户启动应用程序时,随着设备从设备的方向偏移,我将对行为进行调整.因此,我捕获了CMAttitude属性,即referenceAttitude是用户启动应用程序时设备的姿态,然后在后续更新中使用multiplyByInverseOfAttitude来施加相对于原始方向的重力.显然,如果您愿意,也可以使用预定的态度.

Note, in both of those examples, I'm applying the adjustments to the behaviors as the device shifts from the orientation of the device when the user started the app. Thus I capture a CMAttitude property, referenceAttitude to be the attitude of the device when the user fired up the app, and then uses multiplyByInverseOfAttitude on subsequent updates to apply gravity in relation to that original orientation. You could, obviously, use a predetermined attitude if you wanted, too.

但希望以上内容说明了解决此类UX的一种方法.

But hopefully the above illustrates one approach to tackling this sort of UX.

这篇关于“推动"指的是“推".匹配UIInterpolatingMotionEffect? (即,在UIInterpolatingMotionEffect上访问“物理")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆