ios - NSMutableAttributedString strike-through with animation -
- (void)setstrokelabel:(bool)strokelabel { _strokelabel = strokelabel; if (_strokelabel) { _timer = [nstimer scheduledtimerwithtimeinterval:0.4 target:self selector:@selector(setstrokethrough) userinfo:nil repeats:no]; } else { [self cancelstrokethrough]; } } - (void)setstrokethrough { nsmutableattributedstring *attributedstring = [[nsmutableattributedstring alloc] initwithattributedstring:self.attributedtext]; (nsuinteger = 1; <= [attributedstring length]; i++) { [attributedstring addattribute:nsstrikethroughstyleattributename value:[nsnumber numberwithint:1] range:nsmakerange(0, i)]; self.attributedtext = attributedstring; } } - (void)cancelstrokethrough { nsmutableattributedstring *attributedstring = [[nsmutableattributedstring alloc] initwithattributedstring:self.attributedtext]; [attributedstring removeattribute:nsstrikethroughstyleattributename range:nsmakerange(0, [attributedstring length])]; self.attributedtext = attributedstring; }
i want animate strike-through
, todo done animation. set timer it, timer handle how show stoke through letter letter??
here 2 functions job.
bool setstrokethrough(uilabel *label, nsrange range) { if (range.location >= [label.attributedtext length]) return false; if (range.location + range.length > [label.attributedtext length]) range.length = [label.attributedtext length] - range.location; nsmutableattributedstring *attributedstring = [[nsmutableattributedstring alloc] initwithattributedstring:label.attributedtext]; [attributedstring addattribute:nsstrikethroughstyleattributename value:@(nsunderlinestylesingle) range:range]; label.attributedtext = attributedstring; return true; } -(void)animatesetstrokethroughduration:(float)duration { __block float const stepduration = 0.1; float steps = duration / stepduration; __block nsrange range = nsmakerange(0, ceil((float)[self.label.attributedtext length] / steps)); void (^__block fn)(); void (^__block __weak weakfn)(); weakfn = fn = ^(){ if (!setstrokethrough(self.label, range)) return; range = nsmakerange(range.location + range.length, range.length); [self performblock:weakfn afterdelay:stepduration]; }; fn(); }
notes
Comments
Post a Comment