gpt4 book ai didi

ios - iOS动画队列,用于接收消息

转载 作者:行者123 更新时间:2023-12-01 18:58:01 26 4
gpt4 key购买 nike

我有一个简单的动画,当收到消息时会运行。如果他们只收到一条消息,这将非常有用。但是,如果用户在动画中收到多个消息,则动画将重新开始。我不希望这种情况发生,我希望动画在开始下一个动画之前完成。我的想法是创建某种队列,该队列将通过键“from”和“body”在字典中循环,但是我不确定这是否是最佳实践。有人有什么想法吗?

- (void) messageReceived:(NSNotification *)notification
{

NSString *body = [[notification userInfo] valueForKey:@"body"];
NSString *from = [[notification userInfo] valueForKey:@"from"];
UILabel *usernameLabel = (UILabel *)[self.view viewWithTag:502];
usernameLabel.text = from;

UILabel *messageLabel = (UILabel *)[self.view viewWithTag:503];
messageLabel.text = body;
CGRect notificationFrame = notificationView.frame;
notificationFrame.origin.y = 0;

[UIView animateWithDuration:0.3
delay:0.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
notificationView.frame = notificationFrame;
}
completion:^(BOOL finished){

if (finished) {
[UIView animateWithDuration:0.3
delay:1.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{
CGRect oldNotificationFrame = notificationView.frame;
oldNotificationFrame.origin.y = -100;
notificationView.frame = oldNotificationFrame;
}
completion:NULL];
}


}];
}

最佳答案

我认为您需要采取两种方法。创建消息并对其进行排队,然后对消息进行动画处理,除非已将一条或多条消息进行动画处理;在这种情况下,只需将动画排队即可。动画结束时,将递归完成块,直到队列为空。

@implementation yourCLass {

// holds incoming animation requests in FIFO order
NSMutableArray *animationQueue;

// control switch to avoid starting an animation if one is running
BOOL isAnimating;
}

- (void) messageReceived:(NSNotification *)notification {

// pertinent message data is stored in a dict in the array
NSString *body = [[notification userInfo] valueForKey:@"body"];
NSString *from = [[notification userInfo] valueForKey:@"from"];

NSDictionary *messageFacts = @{ @"body" : body,
@"from" : from };

// most-recent messages get put into position zero; LIFO
// NB: this array has already been initialized elsewhere...
[self->animationQueue insertObject:messageFacts
atIndex:0];

// now for animating BUT ONLY if not already animating (if already animating the queued
// animations will get animated anyway)
if (!self->isAnimating) {

[self animateMessages];
}
}

- (void) animateMessages {

if (self->animationQueue.count == 0) {
// all messages have been animated, clear isAnimating bit and exit

self->isAnimating = NO;
return;
}

self->isAnimating = YES;

// extract message data from array as a dictionary, delete from array
NSDictionary *messageFacts = [self->animationQueue lastObject];
[self->animationQueue removeLastObject];

// import message data from dictionary to labels
UILabel *usernameLabel = (UILabel *)[self.view viewWithTag:502];
usernameLabel.text = messageFacts[@"from"];

UILabel *messageLabel = (UILabel *)[self.view viewWithTag:503];
messageLabel.text = messageFacts[@"body"];

// other housekeeping
CGRect notificationFrame = notificationView.frame;
notificationFrame.origin.y = 0;

[UIView animateWithDuration:0.3
delay:0.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{

notificationView.frame = notificationFrame;
}
completion:^(BOOL finished){

if (finished) {

[UIView animateWithDuration:0.3
delay:1.0
options:UIViewAnimationOptionCurveEaseOut
animations:^{

CGRect oldNotificationFrame = notificationView.frame;
oldNotificationFrame.origin.y = -100;
notificationView.frame = oldNotificationFrame;
}
completion:^(BOOL finished){

if (finished ) {

// when done, method recurs until all notifications have
// been animated
[self animateMessages];
}
}];
}
}];
}

关于ios - iOS动画队列,用于接收消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25275889/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com