有没有办法为动画提供动画完成闭包UINavigationController?
我有一条线,如,
navigationController?.setNavigationBarHidden(navigationController?.navigationBarHidden == false, animated: true)
Run Code Online (Sandbox Code Playgroud)
我希望检测它何时完成.
有没有办法实现这一目标?
我已经在我的项目中安装了 Gifu 来管理一些 gif 动画。
有谁知道如何在动画的 1 个循环完成时运行代码?
我似乎让完成处理的代码与动画同时运行。
这是我的代码:
override func viewDidLoad() {
super.viewDidLoad()
animatedDone.animate(withGIFNamed: "animatedTick.gif")
animatedDone.prepareForAnimation(withGIFNamed: "animatedTick.gif", loopCount: 1) { (Void) in
saveAndExit()
}
}
Run Code Online (Sandbox Code Playgroud) 我尝试将起点和终点本地化为地址字符串,以便我可以将其存储到NSUserDefaults.问题是该方法继续执行并且不设置我的变量.
NSLog(@"Begin");
__block NSString *returnAddress = @"";
[self.geoCoder reverseGeocodeLocation:self.locManager.location completionHandler:^(NSArray *placemarks, NSError *error) {
if(error){
NSLog(@"%@", [error localizedDescription]);
}
CLPlacemark *placemark = [placemarks lastObject];
startAddressString = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@",
placemark.subThoroughfare, placemark.thoroughfare,
placemark.postalCode, placemark.locality,
placemark.administrativeArea,
placemark.country];
returnAddress = startAddressString;
//[self.view setUserInteractionEnabled:YES];
}];
NSLog(returnAddress);
NSLog(@"Einde");
Run Code Online (Sandbox Code Playgroud)
这是我的应用程序调试器显示的内容:
启动
einde
例如,我的位置地址是:"Mainstreet 32,CITY".然后我想看到的是以下内容:
启动
Mainstreet 32,CITY
Einde
问题是我的代码不等待我CLGeocoder完成,所以我的变量returnAddress在返回时没有设置,并且它是空的.
有谁知道如何解决这个问题?
我正在制作一个应用程序,其中,我必须使我的Inappurchase产品自动更新,为此,在阅读Apple文档后我才知道,在每次自动更新产品交易后,我们的应用程序收到每次购买的交易收据,我需要在验证我的交易收据应用程序之后验证来自Apple服务器的收据是否必须保存该交易日期.但在购买产品后,当我试图通过Apple Classes -Verification Controller验证Apple服务器的交易收据时,我的应用程序在完成处理程序时崩溃,其显示完成处理程序NIL.
当执行到达任何这些方法时,我的_completionHandlers被释放了什么?请指导我解决这个问题
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSString *responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
// So we got some receipt data. Now does it all check out?
BOOL isOk = [self doesTransactionInfoMatchReceipt:responseString];
VerifyCompletionHandler completionHandler = _completionHandlers[[NSValue valueWithNonretainedObject:connection]];
NSValue *key = [NSValue valueWithNonretainedObject:connection];
NSLog(@"%@",_completionHandlers);
[_completionHandlers removeObjectForKey:key];
if (isOk)
{
//Validation suceeded. Unlock content here.
NSLog(@"Validation successful");
completionHandler(TRUE);
} else {
NSLog(@"Validation failed");
completionHandler(FALSE);
}
}
Run Code Online (Sandbox Code Playgroud) 我有两个功能: func Females_NonChat()和func males_NonChat()
我想等他们都在viewDidLoad中执行打印语句之前完成.我需要另一个完成处理程序才能完成吗?
使用的函数是firebase完成处理程序,用于从在线数据库请求信息...
override func viewDidLoad() {
super.viewDidLoad()
func Females_NonChat()
func males_NonChat()
print("finished executing both asynchronous functions")
}
func Females_NonChat(){
Anon_Ref.child("Chatting").child("female").observeSingleEventOfType(.Value, withBlock: {(snapshot) in
if let FemInChatting = snapshot.value as? [String : String] {
print("executing")
}
})
}
func males_NonChat(){
Anon_Ref.child("Chatting").child("male").observeSingleEventOfType(.Value, withBlock: {(snapshot) in
print("executing")
})
}
Run Code Online (Sandbox Code Playgroud) grand-central-dispatch ios completionhandler swift firebase-realtime-database
我写了很多完成块,但不知道为什么会这样。如果我们使用适当的参数调用块,则基于块的函数的控制不应继续进行。但就我而言,它正在这样做。
- (void) validateFormWithCompletion: (void(^)(BOOL valid)) completion
{
if (! [NetworkConstant appIsConnected])
{
[[AppThemeManager sharedInstance] showNoInternetMessage];
completion(NO);
}
emailIdTF.text = [emailIdTF.text trimWhiteSpaceAndNextLine];
if (emailIdTF.text.length == 0)
{
[[AppThemeManager sharedInstance] showNotificationWithTitle:@"Incomplete" subtitle:@"Please fill in a valid email id" duration:durationForTSMessage withTypeOfNotification:notificationWarning];
completion(NO);
}
else
{
completion(YES);
}
}
Run Code Online (Sandbox Code Playgroud)
如果没有 Internet 连接,则控件应从第一次出现完成 (NO); 时返回。但相反,它前进到电子邮件长度检查。我在这里做错了吗?
我了解到并发DispatchQueue允许其中的代码立即返回,因此不会阻塞调用线程。这通常与加载大数据的后台任务一起使用。
我还了解到完成处理程序(例如, in URLSession)允许在某些任务完成后执行处理程序中的代码。
我的问题是:这是否意味着并发调度队列和完成处理程序具有重叠的目的?如果我已经使用了完成处理程序,就不需要用并发调度队列包装它吗?
比如下面是一个使用 URLSession 的耗时的数据加载任务,用并发调度队列包装它是不是一个好主意?
URLSession(configuration: URLSessionConfiguration.default).dataTask(with: propertiesRequest) { data, response, error in
// print("within dataTask: data: \(data), response: \(response), error: \(error)")
if let error = error {
print(error)
} else if let httpResponse = response as? HTTPURLResponse {
if httpResponse.statusCode == 200 {
print("success: property task request")
do {
handler(responseDict, nil) // This is a function supplied by caller
} catch let error as NSError {
handler(nil, error)
}
}
}
}
Run Code Online (Sandbox Code Playgroud) concurrency grand-central-dispatch completionhandler nsurlsession swift
我有一个在视图上执行动画的函数。我想为这个函数实现一个完成处理程序,它将在动画完成后调用。
在视图控制器...
hudView.hide(animated: true, myCompletionHandler: {
// Animation is complete
})
Run Code Online (Sandbox Code Playgroud)
在 HudView 类中...
func hide(animated: Bool, myCompletionHandler: () -> Void) {
if animated {
transform = CGAffineTransform(scaleX: 0.7, y: 0.7)
UIView.animate(withDuration: 0.3, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0.5, options: [], animations: {
self.alpha = 0
self.transform = CGAffineTransform.identity
}, completion: nil) // I want to run 'myCompletionHandler' in this completion handler
}
}
Run Code Online (Sandbox Code Playgroud)
我尝试了很多东西,但找不到正确的语法:
}, completion: myCompletionHandler)
Run Code Online (Sandbox Code Playgroud)
将非转义参数“myCompletionHandler”传递给需要@escaping 闭包的函数
}, completion: myCompletionHandler())
Run Code Online (Sandbox Code Playgroud)
无法将“Void”类型的值转换为预期的参数类型“((Bool) -> Void)?”
}, completion: { myCompletionHandler() …Run Code Online (Sandbox Code Playgroud) 也许我不理解完成处理程序的概念,但我有一个像这样的函数;
func someFunction(completion: @escaping (Bool, LoginError?) -> Void) {
self.checkDevice() { allowed, error in
if let e = error {
completion(false, e)
}
completion(true, nil)
}
}
Run Code Online (Sandbox Code Playgroud)
虽然不了解具体内容checkDevice(),但基本前提是它执行异步网络调用,并返回true且无错误 (nil),或者返回false(有错误)。
当我运行此代码时,我发现完成处理程序被调用两次。它将完成作为元组 (as false, error) 和 ( true, nil) 发送。我已经做了一些调试,似乎没有办法someFunction()调用两次。
我相信一旦发送完成,函数就会结束。在我的测试用例中,我强制从 中产生错误checkDevice(),这应该会导致我将完成发送为 ( false, error),但我同时看到 ( false, error) 和 ( true, nil)。完成不会立即结束函数吗?
我正在尝试重构我的代码,并想在中返回Bool一个closure。当我尝试它时,它说它是未使用的并且不起作用。我可以用另一种方式来做,但是我要重复不想做的代码。我该怎么办。
func tableView(_ pTableView: UITableView, canEditRowAt pIndexPath: IndexPath) -> Bool {
// These lines are the one that work but would like to get rid of them
if let rowConversation = self.objectAtIndexPath(pIndexPath) as? Conversation {
if rowConversation.isGroupChat && rowConversation.expired {
return true
}
}
self.getRowConversation(pIndexPath: pIndexPath) {
// how to return true here
}
return false
}
private func getRowConversation(pIndexPath: IndexPath, completion pCompletion: () -> Void) {
if let rowConversation = self.objectAtIndexPath(pIndexPath) as? Conversation {
if …Run Code Online (Sandbox Code Playgroud) ios ×7
swift ×7
animation ×2
objective-c ×2
clgeocoder ×1
closures ×1
cocoa-touch ×1
concurrency ×1
crash ×1
iphone ×1
nsurlsession ×1
transactions ×1