我知道那裏有很多第三方解決方案,但我決定自己建立一個,以全面控制動畫並學習新內容。iOS建立自定義評級控制問題
看起來大部分時間都是通過UIControl的子類化和跟蹤觸摸來完成的,但我使用了不同的方法。我用圖像創建系統按鈕。通過這種方式,我可以免費獲得新聞媒體上的精彩動畫。無論如何,它非常好,直到你真的很快按下它。這是一個發生了什麼事情的GIF。
有時候,如果你這樣做真快全明星卡。我會試着解釋這個動畫背後發生的事情。由於滿星和空星具有不同的顏色,因此每當我變滿或變空時,我都會更改按鈕的色調。您看到的動畫是一個單獨的UIImageView,它在動畫塊開始之前的按鈕頂部添加,並在完成塊中刪除。我相信有時清空明星動畫不會被解僱,並且在完成塊中不會刪除圖片。但我無法在代碼中捕捉到這個錯誤。
我添加了一些調試代碼,以查看是否使星空的塊甚至被解僱。似乎是這樣,甚至完成塊被稱爲。
2015-04-23 13:00:00.416 RERatingControl[24011:787202]
Touch number: 5
2015-04-23 13:00:00.416 RERatingControl[24011:787202]
removing a star at index: 4
2015-04-23 13:00:00.693 RERatingControl[24011:787202] star removed
明星指數是從零開始的,所以第5顆星星應該被移除,但事實並非如此。
整個項目在github。這很小。如果有人能幫我解決這個問題,我將不勝感激。哦,如果您想要使用此代碼,請隨時使用:-)
UPDATE。我被建議在這裏發佈一些邏輯代碼。我沒有馬上做,因爲我覺得這太麻煩了。
按鈕操作方法:
- (void)buttonPressed:(id)sender {
static int touchNumber = 0;
NSLog(@"\nTouch number: %d", touchNumber);
touchNumber++;
if (self.isUpdating) {
return;
}
self.updating = YES;
NSInteger index = [self.stars indexOfObject:sender];
__block NSTimeInterval delay = 0;
[self.stars enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
REStarButton *btn = (REStarButton*)obj;
if (idx <= index) {
if (!btn.isFull) {
NSLog(@"\nadding a star at index: %ld", (long)idx);
[btn makeFullWithDelay:delay];
delay += animationDelay;
}
} else {
if (btn.isFull) {
NSLog(@"\nremoving a star at index: %ld", (long)idx);
[btn makeEmptyWithDelay:0];
}
}
}];
if ([self.delegate respondsToSelector:@selector(ratingDidChangeTo:)]) {
[self.delegate ratingDidChangeTo:index + 1];
}
self.updating = NO;
}
空全明星方法:
- (void)makeEmptyWithDelay:(NSTimeInterval)delay {
// if (!self.isFull) {
// return;
// }
self.full = NO;
UIImageView *fullStar = [[UIImageView alloc] initWithImage:self.starImageFull];
[self addSubview:fullStar];
CGFloat xOffset = CGRectGetMidX(self.bounds) - CGRectGetMidX(fullStar.bounds);
CGFloat yOffset = CGRectGetMidY(self.bounds) - CGRectGetMidY(fullStar.bounds);
fullStar.frame = CGRectOffset(fullStar.frame, xOffset, yOffset);
[self setImage:self.starImageEmpty forState:UIControlStateNormal];
[self setTintColor:self.superview.tintColor];
[UIView animateWithDuration:animationDuration delay:delay options:UIViewAnimationOptionCurveEaseOut animations:^{
//fullStar.transform = CGAffineTransformMakeScale(0.1f, 0.1f);
fullStar.transform = CGAffineTransformMakeTranslation(0, 10);
fullStar.alpha = 0;
} completion:^(BOOL finished) {
[fullStar removeFromSuperview];
NSLog(@"star removed");
}];
創建一個空的星充分的方法:
}
- (void)makeFullWithDelay:(NSTimeInterval)delay {
// if (self.isFull) {
// return;
// }
self.full = YES;
UIImageView *fullStar = [[UIImageView alloc] initWithImage:self.starImageFull];
CGFloat xOffset = CGRectGetMidX(self.bounds) - CGRectGetMidX(fullStar.bounds);
CGFloat yOffset = CGRectGetMidY(self.bounds) - CGRectGetMidY(fullStar.bounds);
fullStar.frame = CGRectOffset(fullStar.frame, xOffset, yOffset);
fullStar.transform = CGAffineTransformMakeScale(0.01f, 0.01f);
[self addSubview:fullStar];
[UIView animateKeyframesWithDuration:animationDuration delay:delay options:0 animations:^{
CGFloat ratio = 0.35;
[UIView addKeyframeWithRelativeStartTime:0 relativeDuration:ratio animations:^{
fullStar.transform = CGAffineTransformMakeScale(animationScaleRatio, animationScaleRatio);
}];
[UIView addKeyframeWithRelativeStartTime:ratio relativeDuration:1 - ratio animations:^{
fullStar.transform = CGAffineTransformIdentity;
}];
} completion:^(BOOL finished) {
[self setImage:self.starImageFull forState:UIControlStateNormal];
[self setTintColor:self.fullTintColor];
[fullStar removeFromSuperview];
}];
}
如果您在這裏發佈相關代碼,這將是一件好事。看起來像一個邏輯問題。 – Raptor