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

  1. to put limit on animation time animate blocks of characters, rather charater character. longer string longer block of characters.
  2. __block __weak business explained in here
  3. self performblock extension of nsobject mike ash , here
  4. the code assumes have self.label member defined

Comments

Popular posts from this blog

Delphi XE2 Indy10 udp client-server interchange using SendBuffer-ReceiveBuffer -

Qt ActiveX WMI QAxBase::dynamicCallHelper: ItemIndex(int): No such property in -

Enable autocomplete or intellisense in Atom editor for PHP -