我正在研究核心动画编程指南中的动画,我不得不理解暂停和恢复图层上的动画.
该文档告诉我如何在没有明确解释的情况下暂停和恢复动画.我认为关键是要了解什么是方法timeOffset
和beginTime
方法CAlayer
.
这些代码是暂停和恢复动画.在resumeLayer
方法中,layer.beginTime = timeSincePause;
这条线真的让我感到困惑.
-(void)pauseLayer:(CALayer*)layer {
CFTimeInterval pausedTime = [layer convertTime:CACurrentMediaTime() fromLayer:nil];
layer.speed = 0.0;
layer.timeOffset = pausedTime;
}
-(void)resumeLayer:(CALayer*)layer {
CFTimeInterval pausedTime = [layer timeOffset];
layer.speed = 1.0;
layer.timeOffset = 0.0;
layer.beginTime = 0.0;
CFTimeInterval timeSincePause = [layer convertTime:CACurrentMediaTime() fromLayer:nil] - pausedTime;
layer.beginTime = timeSincePause;
}
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激.
我在UITableView的头文件中找到了这个,几乎每个属性都是非弧形的,虽然我的项目使用的是ARC.
@property (nonatomic, assign) id <UITableViewDataSource> dataSource;
@property (nonatomic, assign) id <UITableViewDelegate> delegate;
Run Code Online (Sandbox Code Playgroud)
为什么Apple不使用weak
属性而不是assign
,它是非弧的向后兼容性?如果是这样,为什么不用__has_feature(objc_arc)
来区分ARC和非ARC.
#if __has_feature(objc_arc)
@property (nonatomic, weak) id <UITableViewDataSource> dataSource;
@property (nonatomic, weak) id <UITableViewDelegate> delegate;
#else
@property (nonatomic, assign) id <UITableViewDataSource> dataSource;
@property (nonatomic, assign) id <UITableViewDelegate> delegate;
#endif
Run Code Online (Sandbox Code Playgroud)
我希望delegate
是弱的,所以当委托实例被释放时我不需要将委托设置为nil.
谢谢你的帮助.
编辑:
我注意到这__has_feature(objc_arc)
是错误的,因为当我的部署目标是4.3时我可以使用ARC,但是我不能使用它weak
.所以条件应该是我的部署目标是否等于5.0或更高.
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_5_0
@property (nonatomic, weak) id <UITableViewDataSource> dataSource;
@property (nonatomic, weak) id <UITableViewDelegate> delegate;
#else
@property (nonatomic, assign) id <UITableViewDataSource> …
Run Code Online (Sandbox Code Playgroud) 在我的应用程序中,我想为每个用户登录将用户设置保存在 plist 文件中,我编写了一个名为的类,该类CCUserSettings
具有几乎相同的界面NSUserDefaults
,它读取和写入与当前用户 ID 相关的 plist 文件。它有效,但性能不佳。每次用户来电[[CCUserSettings sharedUserSettings] synchronize]
,我写了NSMutableDictionary
(这使用户设置)的plist文件,下面的代码显示synchronize
中CCUserSettings
忽略了一些琐碎的细节。
- (BOOL)synchronize {
BOOL r = [_settings writeToFile:_filePath atomically:YES];
return r;
}
Run Code Online (Sandbox Code Playgroud)
我想,NSUserDefaults
当我们调用应该写入文件[[NSUserDefaults standardUserDefaults] synchronize]
,但它运行非常快,我写了一个演示测试,关键部分是下面,运行1000次[[NSUserDefaults standardUserDefaults] synchronize]
,并[[CCUserSettings sharedUserSettings] synchronize]
在我的iPhone6,结果是0.45秒VS9.16秒。
NSDate *begin = [NSDate date];
for (NSInteger i = 0; i < 1000; ++i) {
[[NSUserDefaults standardUserDefaults] setBool:(i%2==1) forKey:@"key"];
[[NSUserDefaults standardUserDefaults] synchronize];
}
NSDate *end = [NSDate date];
NSLog(@"synchronize …
Run Code Online (Sandbox Code Playgroud) 我想要的是重复地在3个值之间动画我的UILabel文本.我要设置的文字是"下载","下载..","下载...",然后重复.有了这个动画,我想让我的用户知道正在完成下载并且应用程序没有堆叠.我一直在谷歌搜索一段时间,但没有找到解决方案.
任何人都可以给我一些参考吗?提前致谢.
在核心动画编程指南中,有一个段落How to Animate Layer-Backed Views
,它说:
如果要使用Core Animation类来启动动画,则必须从基于视图的动画块中发出所有Core Animation调用.UIView类默认禁用图层动画,但在动画块内重新启用它们.因此,您在动画块之外所做的任何更改都不会生成动画.
还有一个例子:
[UIView animateWithDuration:1.0 animations:^{
// Change the opacity implicitly.
myView.layer.opacity = 0.0;
// Change the position explicitly.
CABasicAnimation* theAnim = [CABasicAnimation animationWithKeyPath:@"position"];
theAnim.fromValue = [NSValue valueWithCGPoint:myView.layer.position];
theAnim.toValue = [NSValue valueWithCGPoint:myNewPosition];
theAnim.duration = 3.0;
[myView.layer addAnimation:theAnim forKey:@"AnimateFrame"];
}];
Run Code Online (Sandbox Code Playgroud)
在我看来,它告诉我如果不从基于视图的动画块中发出核心动画调用,就不会有动画.
但似乎如果我直接添加核心动画调用而没有基于视图的动画块,它的工作方式相同.
我错过了什么吗?
我有一个方法createATestObject
.正如其名称所示,它创建一个对象并将其返回.代码很简单,它在ARC下.
- (TestObj *)createATestObj
{
return [[TestObj alloc] init] ;
}
Run Code Online (Sandbox Code Playgroud)
我汇编文件并获得下面的汇编代码.
Lfunc_begin4:
.cfi_startproc
@ BB#0:
push {r7, lr}
mov r7, sp
sub sp, #8
@DEBUG_VALUE: -[ViewController createATestObj]:self <- undef
@DEBUG_VALUE: -[ViewController createATestObj]:_cmd <- undef
str r0, [sp, #4]
str r1, [sp]
movw r0, :lower16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC4_0+4))
movt r0, :upper16:(L_OBJC_CLASSLIST_REFERENCES_$_-(LPC4_0+4))
LPC4_0:
add r0, pc
ldr r0, [r0]
movw r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_10-(LPC4_1+4))
movt r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_10-(LPC4_1+4))
LPC4_1:
add r1, pc
ldr r1, [r1]
blx _objc_msgSend
movw r1, :lower16:(L_OBJC_SELECTOR_REFERENCES_-(LPC4_2+4))
movt r1, :upper16:(L_OBJC_SELECTOR_REFERENCES_-(LPC4_2+4))
LPC4_2:
add …
Run Code Online (Sandbox Code Playgroud) 我使用下面的代码来计算宽度有限的字符串行数.
NSString *text = @"abcdefgh";
UIFont *font = [UIFont systemFontOfSize:15.0];
CGFloat h = [text suggestHeightWithFont:font width:320.0];
NSInteger lines = (h > font.lineHeight) ? h/font.lineHeight+1 : h/font.lineHeight;
NSLog(@"height %f, %f, number lines:%ld", h, font.lineHeight, (unsigned long)lines);
Run Code Online (Sandbox Code Playgroud)
但我发现font.lineHeight
(日志显示它是17.900391)大于设置为的字体大小15.0
.
日志消息显示:
身高17.900391,17.900391,数字线:1
我这样做了但它总是给出错误:
error: use of undeclared identifier 'self'
error: 1 errors parsing expression
Run Code Online (Sandbox Code Playgroud)
在我的应用程序中,我将一些照片保存在沙箱中,并将其绝对路径保存在数据库中。然后,我再次构建并运行,获取路径并尝试获取照片,它说该文件不存在。
最后,我发现每次构建和运行应用程序时,应用程序的路径都是不同的。
我NSTemporaryDirectory()
用来获取临时路径,我运行了两次并获得了两个结果。
/ private / var / mobile / Applications / 80E09BB5-5FEB-4C27-935E-E29DE7861392 / tmp / / private / var / mobile / Applications / 71427100-0DBF-42F0-B6B4-F88F6417292E / tmp /
正常吗 当用户更新我的应用程序时,绝对路径会改变吗?
如果正常,是否应该在数据库中保留相对路径?有一些最佳做法吗?