Objective c 在文本中滑动手势

Objective c 在文本中滑动手势,objective-c,xcode,uilabel,uiswipegesturerecognizer,swipe-gesture,Objective C,Xcode,Uilabel,Uiswipegesturerecognizer,Swipe Gesture,我一直在试图找出一个解决方案,使用向右滑动的手势,使文本出现在标签上。文本具有穿透效果,再次滑动以删除穿透效果,并保持原始文本不变。有没有关于如何做到这一点的代码示例?这是一个XCode问题 if ([***this is the part i need help with***]) { NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:[self.

我一直在试图找出一个解决方案,使用向右滑动的手势,使文本出现在标签上。文本具有穿透效果,再次滑动以删除穿透效果,并保持原始文本不变。有没有关于如何做到这一点的代码示例?这是一个XCode问题

if ([***this is the part i need help with***])
{
    NSMutableAttributedString *attributeString = [[NSMutableAttributedString alloc] initWithString:[self.EditItem valueForKey:@"eventName"]];
    [attributeString addAttribute:NSStrikethroughStyleAttributeName
                            value:@1
                            range:NSMakeRange(0, [attributeString length])];
    self.nameTextField.attributedText = attributeString;
    [self.EditItem setValue:[NSString stringWithFormat:@"Completed"] forKey:@"eventName"];
    NSLog(@"Swiped to the right");
}
else
{
    [NSString initWithString:[self.EditItem valueForKey:@"eventName"]];
    NSLog(@"normal text no strike through");
}
你可以试试这样一个简单的手势识别器

在viewDidLoad或类似文件中添加以下内容

UISwipeGestureRecognizer *swipe = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(strikeThrough)];
swipe.direction = UISwipeGestureRecognizerDirectionRight;

[self.nameTextField addGestureRecognizer:swipe];
然后设置删除线方法将文本更改为删除线-如果只有一个文本字段,只需添加一个切换,以便可以打开和关闭删除线

bool stricken;

- (void) strikeThrough {

    if (stricken) {

        self.nameTextField.text = self.nameTextField.text;

        stricken = false;

    } else {

        NSDictionary* attributes = @{ NSStrikethroughStyleAttributeName: [NSNumber numberWithInt:NSUnderlineStyleSingle] };
        NSAttributedString* attrText = [[NSAttributedString alloc] initWithString:self.nameTextField.text attributes:attributes];
        self.nameTextField.attributedText = attrText;

        stricken = true;

  }

}

这听起来并不难。您遇到的问题是什么?我使用的是if语句,可能还有更好的语句。到目前为止,您能添加代码吗?它将帮助人们回答您的问题。您的代码在第一次刷卡时在文本上添加了删除,再次刷卡不会将文本返回到原始状态。我已更新了我的答案以包含该内容。这是我一直在寻找的,谢谢!如果它对你有帮助,那么将其标记为已接受的答案。