UITest--AccessibilityIdentifier的使用

有时候我们使用ImageView的时候想要知道现在的显示的图片是placeHolder的图片还是加载完成或挑选好的图片,但是tag属性只能拿到却不能判断,当然,加几个bool属性也可以完成,但是会有些复杂,如果可以在给imageview赋图片的时候给图片加个标记,那么下次判断一下标记是不是placeHolder就可以了,很简单。

    

 代码:

 // Swift

 

 

let picView = UIImageView(frame:CGRectMake(10,90,50,50))

        picView.layer.cornerRadius =3

        picView.userInteractionEnabled =true

        picView.layer.masksToBounds =true

        picView.image =UIImage(named:"add_pic.png")

        picView.image?.accessibilityIdentifier ="add"

 

 

这个图片 “add_pic.png”的标记就是  “add”,当你更换picview的image时,如果不给AccessibilityIdentifier属性重新赋值的话,这个属性的值就会变成null,每个图片都会对应一个专属的AccessibilityIdentifier;方便我们识别图片。

 

 // 判断

if picView.image?.accessibilityIdentifier =="add" {

            //是默认图、替换图片

            self.chooseImaeFromAblum({ (image) -> (Void)in

                picView.image = image

                picView.accessibilityIdentifier ="new"

            })

        }else{

            //(ImageView中不是默认图)查看大图或进行其他操作

        }

 

       

// oc

 

UIImageView * picView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 90, 50, 50)];
    picView.layer.cornerRadius = 3;
    picView.userInteractionEnabled = YES;
    picView.layer.masksToBounds = YES;
    picView.image = [UIImageimageNamed:@"add_pic.png"];
    [picView.imagesetAccessibilityIdentifier:@"add”];

 

if ([picView.image.accessibilityIdentifierisEqualToString:@"add"]) {
       [self chooseImageFromAblum:^(UIImage*image) {
        picView.image = image;
// 给新图片的AccessibilityIdentifier赋新值
        [picView.imagesetAccessibilityIdentifier:@"new"];
    }]; 
    }else{
        [self blowUpImageWithPic:picView.image];
    } 

 

 

这样会方便很多,减少bool变量过多带来的问题。

 

换成其他的属性也可以:

 

 

- (void)moveFromOtherCell:(UIView *)currentResponder

{

    if ([self.numberOfPlansHourTF.accessibilityIdentifier isEqualToString:currentResponder.accessibilityIdentifier])

    {

        [self.numberOfPlansHourTF becomeFirstResponder];

    }

    else if ([self.numberOfResultsHourTF.accessibilityIdentifier isEqualToString:currentResponder.accessibilityIdentifier])

    {

        [self.numberOfResultsHourTF becomeFirstResponder];

    }

}

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值