- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在我的应用程序中,我使用包含 UICollisionBehavior 的 UIKit Dynamics 使菜单在打开和关闭时弹跳。我为此使用的代码如下。这在 iOS8 上运行良好。然而,对于 iOS9(包括刚刚发布的 iOS9.1 beta 2),我发现了一个奇怪的问题。从表面上看,我用这个弹跳动画弹跳的菜单在打开然后关闭后并没有完全关闭。仔细观察,我发现 UICollisionBehavior 的边界在 iOS8 和 iOS9 中使用相同的值计算。
菜单打开碰撞边界:(798,330) 到 (1024,330)
它代表屏幕上的一条线,菜单底部在打开和弹跳后最终应该停留在该位置。
菜单关闭碰撞边界:(798,-280) 到 (1024,-280)
它代表一条线,在屏幕外,菜单顶部在关闭和弹跳后最终应该停留在该位置。
问题出现在 iOS9 中,菜单 UIView 实际上并没有最终停留在这些边界上。打开后,iOS9 的菜单框是这样的:
(798, -1.5; 226, 330)
[这是:(x, y; w, h)]
关闭后,菜单框如下所示:
(798, -278.5; 226, 330)
但是,这实际上应该是:
(798, 0; 226, 330)
(打开后)
(798, -280; 226, 330)
(关闭后)
还有其他人看到 iOS9 和碰撞行为的这些问题吗?
我打算在我的代码中添加一个 hack(在下面搜索“HACK”),我会针对 iOS9 选择性地使用它,但我真的不喜欢这些 hack!
弹跳动画.h
//
// BounceAnimation.h
// Petunia
//
// Created by Christopher Prince on 12/18/14.
// Copyright (c) 2014 Spastic Muffin, LLC. All rights reserved.
//
// Animates an object through a straight line path, up, down, left or right until it lands, after which it bounces. This requires iOS8 or later.
#import <Foundation/Foundation.h>
@interface BounceAnimation : NSObject
// distnace is for the viewToAnimate to travel until it lands and bounces, in points. You must set this before calling run.
- (instancetype) initWithReferenceView: (UIView *) referenceView viewToAnimate: (UIView *) viewToAnimate andDistance: (CGFloat) distance;
// Direction and distance will be obtained from animateToPoint, and should be consistent with the constraints for the direction property below. I.e., the animateToPoint should be down, left, right, or up from the origin of the viewToAnimate.
- (instancetype) initWithReferenceView: (UIView *) referenceView viewToAnimate: (UIView *) viewToAnimate andFinalPoint: (CGPoint) animateToPoint;
// One shot animation. You can only call run once.
- (void) run;
// Called when animation completes, if given. Called when all the bouncing is done.
@property (nonatomic, strong) void (^completion)(void);
// Called when first contact is made with the boundary, just as the first bounce is about to begin.
@property (nonatomic, strong) void (^firstImpactCallback)(void);
// Only keeps weak references to the views passed in the init method.
@property (nonatomic, weak, readonly) UIView *referenceView;
@property (nonatomic, weak, readonly) UIView *viewToAnimate;
// Rate at which the object accelerates towards the boundary. Same units as magnitude for UIGravityBehavior. Defaults to 1.0.
@property (nonatomic) CGFloat accelerationRate;
// Defaults to 0. Units are points per second.
@property (nonatomic) CGFloat initialVelocity;
// This is a unit vector.
// dx (first component) is rightwards; e.g., dx=0, no right/left; dx=-1, is left one unit
// dy (second component) is downwards; e.g., dy=1, down one unit.
// Defaults to (0, 1), downwards.
// Right now, dx and dy can be 0, 1, or -1. One of dx and dy must be 0.
@property (nonatomic, readonly) CGVector direction;
@end
弹跳动画.m
//
// BounceAnimation.m
// Petunia
//
// Created by Christopher Prince on 12/18/14.
// Copyright (c) 2014 Spastic Muffin, LLC. All rights reserved.
//
#import "BounceAnimation.h"
#import "Vector.h"
#import "UIDevice+Extras.h"
@interface BounceAnimation()<UIDynamicAnimatorDelegate, UICollisionBehaviorDelegate>
{
UIDynamicAnimator *_animator;
UIGravityBehavior *_gravityBehavior;
UICollisionBehavior *_collision;
UIDynamicItemBehavior *_velocity;
CGPoint _linearVelocity;
CGFloat _distanceInPoints;
BOOL _calledFirstImpactCallback;
}
@property (nonatomic, weak) UIView *referenceView;
@property (nonatomic, weak) UIView *viewToAnimate;
@property (nonatomic) CGVector direction;
@end
#define INITIAL_GRAVITY_MAGNITUDE 1.0
@implementation BounceAnimation
- (void) setupWithReferenceView: (UIView *) referenceView andViewToAnimate: (UIView *) viewToAnimate;
{
AssertIf([UIDevice ios7OrEarlier], @"Don't have at least iOS8!");
self.referenceView = referenceView;
self.viewToAnimate = viewToAnimate;
_animator = [[UIDynamicAnimator alloc] initWithReferenceView:referenceView];
_animator.delegate = self;
_gravityBehavior = [[UIGravityBehavior alloc] initWithItems:@[viewToAnimate]];
_gravityBehavior.magnitude = INITIAL_GRAVITY_MAGNITUDE;
_velocity = [[UIDynamicItemBehavior alloc] initWithItems:@[viewToAnimate]];
}
// referenceView is just the view on top of which we're doing our animation. E.g., it could be self.view of a view controller. viewToAnimate must be a subview of the reference view.
- (instancetype) initWithReferenceView: (UIView *) referenceView viewToAnimate: (UIView *) viewToAnimate andDistance: (CGFloat) distanceInPoints;
{
self = [super init];
if (self) {
AssertIf(distanceInPoints <= 0.0, @"Invalid distance: %f", _distanceInPoints);
_distanceInPoints = distanceInPoints;
[self setupWithReferenceView:referenceView andViewToAnimate:viewToAnimate];
[self setDirection:CGVectorMake(0.0, 1.0)];
}
return self;
}
- (instancetype) initWithReferenceView: (UIView *) referenceView viewToAnimate: (UIView *) viewToAnimate andFinalPoint: (CGPoint) animateToPoint;
{
self = [super init];
if (self) {
// Need to compute distance and direction.
CGVector direction = [Vector subFirst:[Vector fromPoint:animateToPoint] from:[Vector fromPoint:viewToAnimate.frameOrigin]];
SPASLogDetail(@"direction after subtraction: %@", NSStringFromCGVector(direction));
// Special case: No direction because same start and finish.
if (direction.dy + direction.dx == 0.0) {
_distanceInPoints = 0.0;
}
else {
direction = [Vector normalize:direction]; // vectorNormalize(direction);
_distanceInPoints = [Vector distanceFromPoint:viewToAnimate.frameOrigin toPoint:animateToPoint];
}
SPASLogDetail(@"finalPoint: %@, direction: %@, distance: %f", NSStringFromCGPoint(animateToPoint), NSStringFromCGVector(direction), _distanceInPoints);
[self setupWithReferenceView:referenceView andViewToAnimate:viewToAnimate];
[self setDirection:direction];
}
return self;
}
- (void) setInitialVelocity:(CGFloat)initialVelocity;
{
_initialVelocity = initialVelocity;
// Only positive speeds in the velocity are relevant. Negative speeds reduce the velocity, they don't go the other direction.
_linearVelocity =
CGPointMake(initialVelocity * fabs(_direction.dx),
initialVelocity * fabs(_direction.dy));
}
- (void) setAccelerationRate:(CGFloat)accelerationRate;
{
_accelerationRate = accelerationRate;
_gravityBehavior.magnitude = accelerationRate;
}
// I'm only doing left, right, up, down animations because of the problem of rotating the viewToAnimate. I'm not sure I'll ever have a case where I want a rotated animated view. (Hmmm. If I want to do some kind of continuous animation, arbitrary direction with non-rotated objects could be cool!)
- (void) setDirection:(CGVector)direction;
{
if (_collision) {
[_animator removeBehavior:_collision];
_collision = nil;
}
if (_distanceInPoints == 0.0) {
// Why bother?
SPASLogDetail(@"Zero distance");
return;
}
// 9/24/15; HACK
//_distanceInPoints += 1.5;
// Since we're doing vector operations with one of the init methods above, the following seems risky!
//AssertIf(direction.dy != 0.0 && direction.dy != -1.0 && direction.dy != 1.0, @"Invalid dy: %f", direction.dy);
//AssertIf(direction.dx != 0.0 && direction.dx != -1.0 && direction.dx != 1.0, @"Invalid dx: %f", direction.dx);
_direction = direction;
_gravityBehavior.gravityDirection = direction;
_collision = [[UICollisionBehavior alloc] initWithItems:@[self.viewToAnimate]];
_collision.collisionDelegate = self;
CGPoint startBoundary;
CGPoint endBoundary;
#define SMALL_VALUE 0.05
BOOL (^closeToZero)(CGFloat) = ^(CGFloat value) {
if (value > -SMALL_VALUE && value < SMALL_VALUE) {
return YES;
}
else {
return NO;
}
};
if (closeToZero(direction.dx)) {
// Vertical motion.
CGFloat yBoundary = direction.dy * _distanceInPoints + self.viewToAnimate.frameY;
if (direction.dy > 0.0) {
// If we're going down, then we need to add the height of the self.viewToAnimate to our boundary. This is because the origin coords are in the *upper*, left of the viewToAnimate.
yBoundary += self.viewToAnimate.frameHeight;
}
startBoundary = CGPointMake(self.viewToAnimate.frameX, yBoundary);
endBoundary = CGPointMake(self.viewToAnimate.frameX + self.viewToAnimate.frameWidth, yBoundary);
}
else {
// Horizontal motion.
CGFloat xBoundary = direction.dx * _distanceInPoints + self.viewToAnimate.frameX;
if (direction.dx > 0.0) {
// If we're going to the right, then we need to add the width of the self.viewToAnimate to our boundary. This is because the origin coords are in the upper, *left* of the viewToAnimate.
xBoundary += self.viewToAnimate.frameWidth;
}
startBoundary = CGPointMake(xBoundary, self.viewToAnimate.frameY);
endBoundary = CGPointMake(xBoundary, self.viewToAnimate.frameY + self.viewToAnimate.frameHeight);
}
SPASLog(@"startBoundary: %@, endBoundary: %@", NSStringFromCGPoint(startBoundary), NSStringFromCGPoint(endBoundary));
[_collision addBoundaryWithIdentifier:@"barrier"
fromPoint:startBoundary
toPoint:endBoundary];
[_animator addBehavior:_collision];
}
- (void) run;
{
if (_collision) {
[_animator addBehavior:_gravityBehavior];
[_velocity addLinearVelocity:_linearVelocity forItem:self.viewToAnimate];
[_animator addBehavior:_velocity];
}
else {
if (self.completion) {
self.completion();
}
}
}
#pragma mark - UIDynamicAnimatorDelegate methods
- (void)dynamicAnimatorDidPause:(UIDynamicAnimator*)animator;
{
if (self.completion) {
self.completion();
}
}
#pragma mark -
#pragma mark - UICollisionBehaviorDelegate methods
// This isn't the method that gets called in our case.
//- (void)collisionBehavior:(UICollisionBehavior*)behavior beganContactForItem:(id <UIDynamicItem>)item1 withItem:(id <UIDynamicItem>)item2 atPoint:(CGPoint)p;
- (void)collisionBehavior:(UICollisionBehavior*)behavior beganContactForItem:(id <UIDynamicItem>)item withBoundaryIdentifier:(id <NSCopying>)identifier atPoint:(CGPoint)p;
{
if (!_calledFirstImpactCallback) {
_calledFirstImpactCallback = YES;
if (self.firstImpactCallback) {
self.firstImpactCallback();
}
}
}
#pragma mark -
@end
最佳答案
我在 Swift 中工作,我看到的行为与您在 1.5 点偏移时所做的行为相同。它似乎与设备的分辨率无关(1x、2x 或 3x),都有 1.5 点偏移。
但是,使用基于路径的 API,问题似乎不再存在:
let collisionBehavior: UICollisionBehavior = ...
let topLeft: CGPoint = ...
let bottomLeft: CGPoint = ...
let path = CGPathCreateMutable()
CGPathMoveToPoint(path, nil, topLeft.x, topLeft.y)
CGPathAddLineToPoint(path, nil, bottomLeft.x, bottomLeft.y)
let bezierPath = UIBezierPath(CGPath: path)
collisionBehavior.addBoundaryWithIdentifier("myID", forPath: bezierPath)
将其转换为 Objective-C 非常简单:
UICollisionBehavior *collisionBehavior = ...
CGPoint topLeft = ...
CGPoint bottomLeft = ...
CGMutablePathRef path = CGPathCreateMutable();
CGPathMoveToPoint(path, nil, topLeft.x, topLeft.y);
CGPathAddLineToPoint(path, nil, bottomLeft.x, bottomLeft.y);
UIBezierPath *bezierPath = [UIBezierPath bezierPathWithCGPath: path];
[collisionBehavior addBoundaryWithIdentifier: @"myID" forPath: path];
关于ios8 - iOS9 的 UICollisionBehavior 似乎在距边界 : My bug or Apple's? 1.5 点处停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32771325/
IO 设备如何知道属于它的内存中的值在memory mapped IO 中发生了变化? ? 例如,假设内存地址 0 专用于保存 VGA 设备的背景颜色。当我们更改 memory[0] 中的值时,VGA
我目前正在开发一个使用Facebook sdk登录(通过FBLoginView)的iOS应用。 一切正常,除了那些拥有较旧版本的facebook的人。 当他们按下“使用Facebook登录”按钮时,他
假设我有: this - is an - example - with some - dashesNSRange将使用`rangeOfString:@“-”拾取“-”的第一个实例,但是如果我只想要最后
Card.io SDK提供以下详细信息: 卡号,有效期,月份,年份,CVV和邮政编码。 如何从此SDK获取国家名称。 - (void)userDidProvideCreditCardInfo:(Car
iOS 应用程序如何从网络服务下载图片并在安装过程中将它们安装到用户的 iOS 设备上?可能吗? 最佳答案 您无法控制应用在用户设备上的安装,因此无法在安装过程中下载其他数据。 只需在安装后首次启动应
我曾经开发过一款企业版 iOS 产品,我们公司曾将其出售给大型企业,供他们的员工使用。 该应用程序通过 AppStore 提供,企业用户获得了公司特定的配置文件(包含应用程序配置文件)以启用他们有权使
我正在尝试将 Card.io SDK 集成到我的 iOS 应用程序中。我想为 CardIO ui 做一个简单的本地化,如更改取消按钮标题或“在此保留信用卡”提示文本。 我在 github 上找到了这个
我正在使用 CardIOView 和 CardIOViewDelegate 类,没有可以设置为 YES 的 BOOL 来扫描 collectCardholderName。我可以看到它在 CardIOP
我有一个集成了通话工具包的 voip 应用程序。每次我从我的 voip 应用程序调用时,都会在 native 电话应用程序中创建一个新的最近通话记录。我在 voip 应用程序中也有自定义联系人(电话应
iOS 应用程序如何知道应用程序打开时屏幕上是否已经有键盘?应用程序运行后,它可以接收键盘显示/隐藏通知。但是,如果应用程序在分屏模式下作为辅助应用程序打开,而主应用程序已经显示键盘,则辅助应用程序不
我在模拟器中收到以下错误: ImageIO: CGImageReadSessionGetCachedImageBlockData *** CGImageReadSessionGetCachedIm
如 Apple 文档所示,可以通过 EAAccessory Framework 与经过认证的配件(由 Apple 认证)进行通信。但是我有点困惑,因为一些帖子告诉我它也可以通过 CoreBluetoo
尽管现在的调试器已经很不错了,但有时找出应用程序中正在发生的事情的最好方法仍然是古老的 NSLog。当您连接到计算机时,这样做很容易; Xcode 会帮助弹出日志查看器面板,然后就可以了。当您不在办公
在我的 iOS 应用程序中,我定义了一些兴趣点。其中一些有一个 Kontakt.io 信标的名称,它绑定(bind)到一个特定的 PoI(我的意思是通常贴在信标标签上的名称)。现在我想在附近发现信标,
我正在为警报提示创建一个 trigger.io 插件。尝试从警报提示返回数据。这是我的代码: // Prompt + (void)show_prompt:(ForgeTask*)task{
您好,我是 Apple iOS 的新手。我阅读并搜索了很多关于推送通知的文章,但我没有发现任何关于 APNS 从 io4 到 ios 6 的新更新的信息。任何人都可以向我提供 APNS 如何在 ios
UITabBar 的高度似乎在 iOS 7 和 8/9/10/11 之间发生了变化。我发布这个问题是为了让其他人轻松找到答案。 那么:在 iPhone 和 iPad 上的 iOS 8/9/10/11
我想我可以针对不同的 iOS 版本使用不同的 Storyboard。 由于 UI 的差异,我将创建下一个 Storyboard: Main_iPhone.storyboard Main_iPad.st
我正在写一些东西,我将使用设备的 iTunes 库中的一部分音轨来覆盖 2 个视频的组合,例如: AVMutableComposition* mixComposition = [[AVMutableC
我创建了一个简单的 iOS 程序,可以顺利编译并在 iPad 模拟器上运行良好。当我告诉 XCode 4 使用我连接的 iPad 设备时,无法编译相同的程序。问题似乎是当我尝试使用附加的 iPad 时
我是一名优秀的程序员,十分优秀!