iOS判断字符串中含某一个字符if ([keyName rangeOfString:@"_"].location==NSNotFound) return keyName;

解析Objective-C与Swift在iOS开发中的应用
本文深入探讨了Objective-C与Swift在iOS开发领域的应用与对比,揭示了这两种语言在iOS生态中的角色与优势,为开发者提供选择语言的依据。

if ([keyName rangeOfString:@"_"].location==NSNotFound){

    return keyName;

else{

    NSLog(@"不存在某一字符");

}

/* * Copyright (c) Huawei Technologies Co., Ltd. 2024-2024. All rights reserved. */ import { TouchpadKbStyle } from '../../common/config/KeyStyle'; import { KeyboardThemeStyle } from '../../common/config/ThemeStyle'; import { Constants, KeySpacingType, ThemeType } from '../../common/constants/Constants'; import VibratorController from '../../common/entity/VibratorController'; import DisplayAdaptation from '../../common/utils/DisplayAdaptation'; import { PersistenceUtil } from '../../common/utils/PersistenceUtils'; import { emitter } from '@kit.BasicServicesKit'; import { VKeyboardAction } from '../../common/utils/VKeyboardDevice'; import { LengthMetrics } from '@kit.ArkUI'; // 贴底固定态触控板 const TAG: string = 'TouchpadItem'; @Preview @Component export struct TouchpadItem { private static readonly ANIMATION_DURATION: number = 50; @StorageProp('keySpacingType') keySpacingType: KeySpacingType = PersistenceUtil.getDataSync('keySpacingType', KeySpacingType.WIDE) as KeySpacingType; @StorageProp('themeType') themeType: ThemeType = PersistenceUtil.getDataSync('themeType', ThemeType.BLACK_GOLD) as ThemeType; @State tpBtnLeftIsPressed: boolean = false; @State tpBtnRightIsPressed: boolean = false; @Consume curDensityPixels: number; private pressAnimation: AnimateParam = { duration: TouchpadItem.ANIMATION_DURATION, curve: Curve.Linear, playMode: PlayMode.Normal }; private getAdaptedValue(value: number): number { return DisplayAdaptation.getCurAdaptedValue(value, this.curDensityPixels); } onCustomKeyEvent = (eventData: emitter.EventData) => { if (eventData.data?.vkeyboardAction === VKeyboardAction.RESET_BUTTON_COLOR) { setTimeout(() => { this.tpBtnLeftIsPressed = false; this.tpBtnRightIsPressed = false; }, TouchpadItem.ANIMATION_DURATION); } else if (Constants.ID_TOUCHPAD_LEFT_CLICK_AREA === eventData?.data?.keyName) { if (eventData.data?.vkeyboardAction === VKeyboardAction.VKEY_DOWN) { this.tpBtnLeftIsPressed = true; VibratorController.startVibrate(); } else if (eventData.data?.vkeyboardAction === VKeyboardAction.VKEY_UP) { setTimeout(() => { this.tpBtnLeftIsPressed = false; }, TouchpadItem.ANIMATION_DURATION); } } else if (Constants.ID_TOUCHPAD_RIGHT_CLICK_AREA === eventData?.data?.keyName) { if (eventData.data?.vkeyboardAction === VKeyboardAction.VKEY_DOWN) { this.tpBtnRightIsPressed = true; VibratorController.startVibrate(); } else if (eventData.data?.vkeyboardAction === VKeyboardAction.VKEY_UP) { setTimeout(() => { this.tpBtnRightIsPressed = false; }, TouchpadItem.ANIMATION_DURATION); } } } @Builder overlayImage() { if (KeyboardThemeStyle.getThemeStyle(this.themeType).touchpadBackgroundResource && KeyboardThemeStyle.getThemeStyle(this.themeType).touchpadBackgroundNarrowResource) { Image($r(this.keySpacingType === KeySpacingType.WIDE ? KeyboardThemeStyle.getThemeStyle(this.themeType).touchpadBackgroundResource : KeyboardThemeStyle.getThemeStyle(this.themeType).touchpadBackgroundNarrowResource)) .width(this.getAdaptedValue(TouchpadKbStyle.TOUCHPAD_WIDTH)) .height(this.keySpacingType == KeySpacingType.WIDE ? this.getAdaptedValue(TouchpadKbStyle.WIDE_DB_TP_HEIGHT) : this.getAdaptedValue(TouchpadKbStyle.NARROW_KB_TP_HEIGHT)) .objectFit(ImageFit.Fill) .syncLoad(true) } } aboutToAppear(): void { emitter.on('keyEvent', this.onCustomKeyEvent); } aboutToDisappear(): void { emitter.off('keyEvent', this.onCustomKeyEvent); } build() { Flex({ alignItems: ItemAlign.End, justifyContent: FlexAlign.End, direction: FlexDirection.Column }) { if (KeyboardThemeStyle.getThemeStyle(this.themeType).trackpadGradient && KeyboardThemeStyle.getThemeStyle(this.themeType).trackpadBorderGradient) { Flex({ justifyContent: FlexAlign.Center }) { Flex() { Blank() } .linearGradient({ angle: 180, colors: KeyboardThemeStyle.getThemeStyle(this.themeType).trackpadGradient }) .width(Constants.ALL_PERCENT) .height(Constants.ALL_PERCENT) .margin(this.getAdaptedValue(1)) .borderRadius({ topLeft: this.getAdaptedValue(KeyboardThemeStyle.getThemeStyle(this.themeType).trackpadTopLeftRadius ?? TouchpadKbStyle.TOUCHPAD_BTN_CORNER_RADIUS), topRight: this.getAdaptedValue(KeyboardThemeStyle.getThemeStyle(this.themeType).trackpadTopRightRadius ?? TouchpadKbStyle.TOUCHPAD_BTN_CORNER_RADIUS) }) .borderColor(Constants.TRANSPARENT_BACKGROUND) } .id(Constants.ID_TOUCHPAD_TOUCH_AREA_MAIN) .width(this.getAdaptedValue(TouchpadKbStyle.TOUCHPAD_WIDTH)) .height(this.keySpacingType == KeySpacingType.WIDE ? this.getAdaptedValue(TouchpadKbStyle.WIDE_DB_TP_HEIGHT_MAIN) : this.getAdaptedValue(TouchpadKbStyle.NARROW_KB_TP_HEIGHT_MAIN)) .borderRadius({ topLeft: this.getAdaptedValue(KeyboardThemeStyle.getThemeStyle(this.themeType).trackpadTopLeftRadius ?? TouchpadKbStyle.TOUCHPAD_BTN_CORNER_RADIUS), topRight: this.getAdaptedValue(KeyboardThemeStyle.getThemeStyle(this.themeType).trackpadTopRightRadius ?? TouchpadKbStyle.TOUCHPAD_BTN_CORNER_RADIUS) }) .linearGradient({ angle: 180, colors: KeyboardThemeStyle.getThemeStyle(this.themeType).trackpadBorderGradient }) } else { Rect() .id(Constants.ID_TOUCHPAD_TOUCH_AREA_MAIN) .width(this.getAdaptedValue(TouchpadKbStyle.TOUCHPAD_WIDTH)) .height(this.keySpacingType == KeySpacingType.WIDE ? this.getAdaptedValue(TouchpadKbStyle.WIDE_DB_TP_HEIGHT_MAIN) : this.getAdaptedValue(TouchpadKbStyle.NARROW_KB_TP_HEIGHT_MAIN)) .radius( KeyboardThemeStyle.getThemeStyle(this.themeType).trackpadTopLeftRadius && KeyboardThemeStyle.getThemeStyle(this.themeType).trackpadTopRightRadius ? [[this.getAdaptedValue(KeyboardThemeStyle.getThemeStyle(this.themeType).trackpadTopLeftRadius), this.getAdaptedValue(KeyboardThemeStyle.getThemeStyle(this.themeType).trackpadTopLeftRadius)], [this.getAdaptedValue(KeyboardThemeStyle.getThemeStyle(this.themeType).trackpadTopRightRadius), this.getAdaptedValue(KeyboardThemeStyle.getThemeStyle(this.themeType).trackpadTopRightRadius)]] : [[this.getAdaptedValue(TouchpadKbStyle.TOUCHPAD_BTN_CORNER_RADIUS), this.getAdaptedValue(TouchpadKbStyle.TOUCHPAD_BTN_CORNER_RADIUS)], [this.getAdaptedValue(TouchpadKbStyle.TOUCHPAD_BTN_CORNER_RADIUS), this.getAdaptedValue(TouchpadKbStyle.TOUCHPAD_BTN_CORNER_RADIUS)]]) .fill(KeyboardThemeStyle.getThemeStyle(this.themeType).touchpadBackgroundColor) } // 如果有触控板的边框图 用触控板背景颜色做边框 if (KeyboardThemeStyle.getThemeStyle(this.themeType).fillDivider) { Blank() .height(this.getAdaptedValue(TouchpadKbStyle.TOUCHPAD_BTN_DIVIDER_HORIZONTAL_HEIGHT)) .borderWidth(0) .backgroundColor(KeyboardThemeStyle.getThemeStyle(this.themeType).touchpadBackgroundColor) } else { Blank() .height(this.getAdaptedValue(TouchpadKbStyle.TOUCHPAD_BTN_DIVIDER_HORIZONTAL_HEIGHT)) .borderWidth(0) } // 如果触控板需要使用边框图做 需要提供宽窄两种触控板的图 和 触控板按压色 if (KeyboardThemeStyle.getThemeStyle(this.themeType).touchpadBackgroundResource && KeyboardThemeStyle.getThemeStyle(this.themeType).touchpadBackgroundNarrowResource && KeyboardThemeStyle.getThemeStyle(this.themeType).touchpadPressedColor) { Flex({ alignItems: ItemAlign.Start, justifyContent: FlexAlign.Center }) { Button() .id(Constants.ID_TOUCHPAD_LEFT_CLICK_AREA) .height(this.getAdaptedValue(TouchpadKbStyle.TOUCHPAD_BTN_HEIGHT)) .type(ButtonType.Normal) .flexGrow(1) .animation(this.pressAnimation) .linearGradient({ angle: KeyboardThemeStyle.getThemeStyle(this.themeType).touchpadLeftPressedAngle, colors: this.tpBtnLeftIsPressed ? KeyboardThemeStyle.getThemeStyle(this.themeType).touchpadPressedColor : [[KeyboardThemeStyle.getThemeStyle(this.themeType).touchpadBackgroundColor, 1]] }) .backgroundColor(KeyboardThemeStyle.getThemeStyle(this.themeType).touchpadBackgroundColor) .stateEffect(false) .margin({ end: KeyboardThemeStyle.getThemeStyle(this.themeType).fillDivider ? LengthMetrics.vp(0) : LengthMetrics.vp(this.getAdaptedValue(TouchpadKbStyle.TOUCHPAD_BTN_DIVIDER_HORIZONTAL_HEIGHT)) }) .borderRadius({ bottomLeft: this.getAdaptedValue(KeyboardThemeStyle.getThemeStyle(this.themeType).trackpadBottomLeftRadius ?? TouchpadKbStyle.TOUCHPAD_BTN_CORNER_RADIUS) }) .direction(Direction.Ltr) Button() .id(Constants.ID_TOUCHPAD_RIGHT_CLICK_AREA) .height(this.getAdaptedValue(TouchpadKbStyle.TOUCHPAD_BTN_HEIGHT)) .type(ButtonType.Normal) .flexGrow(1) .animation(this.pressAnimation) .linearGradient({ angle: KeyboardThemeStyle.getThemeStyle(this.themeType).touchpadRightPressedAngle, colors: this.tpBtnRightIsPressed ? KeyboardThemeStyle.getThemeStyle(this.themeType).touchpadPressedColor : [[KeyboardThemeStyle.getThemeStyle(this.themeType).touchpadBackgroundColor, 1]] }) .backgroundColor(KeyboardThemeStyle.getThemeStyle(this.themeType).touchpadBackgroundColor) .stateEffect(false) .borderRadius({ bottomRight: this.getAdaptedValue(KeyboardThemeStyle.getThemeStyle(this.themeType).trackpadBottomRightRadius ?? TouchpadKbStyle.TOUCHPAD_BTN_CORNER_RADIUS) }) .direction(Direction.Ltr) } .backgroundColor(KeyboardThemeStyle.getThemeStyle(this.themeType).fillDivider ? KeyboardThemeStyle.getThemeStyle(this.themeType).touchpadBackgroundColor : undefined) .borderRadius({ bottomLeft: this.getAdaptedValue(KeyboardThemeStyle.getThemeStyle(this.themeType).trackpadBottomLeftRadius ?? TouchpadKbStyle.TOUCHPAD_BTN_CORNER_RADIUS), bottomRight: this.getAdaptedValue(KeyboardThemeStyle.getThemeStyle(this.themeType).trackpadBottomRightRadius ?? TouchpadKbStyle.TOUCHPAD_BTN_CORNER_RADIUS) }) } else { Flex({ alignItems: ItemAlign.Start, justifyContent: FlexAlign.Center }) { Button({ type: ButtonType.Normal }) .id(Constants.ID_TOUCHPAD_LEFT_CLICK_AREA) .height(this.getAdaptedValue(TouchpadKbStyle.TOUCHPAD_BTN_HEIGHT)) .flexGrow(1) .animation(this.pressAnimation) .backgroundColor(KeyboardThemeStyle.getThemeStyle(this.themeType).touchpadBackgroundColor) .scale(this.tpBtnLeftIsPressed ? { x: 0.95, y: 0.95 } : { x: 1, y: 1 }) .stateEffect(false) .borderRadius({ bottomLeft: this.getAdaptedValue(KeyboardThemeStyle.getThemeStyle(this.themeType).trackpadBottomLeftRadius ?? TouchpadKbStyle.TOUCHPAD_BTN_CORNER_RADIUS) }) .margin({ end: LengthMetrics.vp(this.getAdaptedValue(TouchpadKbStyle.TOUCHPAD_BTN_DIVIDER_HORIZONTAL_HEIGHT)) }) .direction(Direction.Ltr) Button({ type: ButtonType.Normal }) .id(Constants.ID_TOUCHPAD_RIGHT_CLICK_AREA) .height(this.getAdaptedValue(TouchpadKbStyle.TOUCHPAD_BTN_HEIGHT)) .flexGrow(1) .animation(this.pressAnimation) .backgroundColor(KeyboardThemeStyle.getThemeStyle(this.themeType).touchpadBackgroundColor) .scale(this.tpBtnRightIsPressed ? { x: 0.95, y: 0.95 } : { x: 1, y: 1 }) .stateEffect(false) .borderRadius({ bottomRight: this.getAdaptedValue(KeyboardThemeStyle.getThemeStyle(this.themeType).trackpadBottomRightRadius ?? TouchpadKbStyle.TOUCHPAD_BTN_CORNER_RADIUS) }) .direction(Direction.Ltr) } .borderRadius({ bottomLeft: this.getAdaptedValue(KeyboardThemeStyle.getThemeStyle(this.themeType).trackpadBottomLeftRadius ?? TouchpadKbStyle.TOUCHPAD_BTN_CORNER_RADIUS), bottomRight: this.getAdaptedValue(KeyboardThemeStyle.getThemeStyle(this.themeType).trackpadBottomRightRadius ?? TouchpadKbStyle.TOUCHPAD_BTN_CORNER_RADIUS) }) .direction(Direction.Ltr) } } .id(Constants.ID_TOUCHPAD_TOUCH_AREA) .width(this.getAdaptedValue(TouchpadKbStyle.TOUCHPAD_WIDTH)) .height(this.keySpacingType == KeySpacingType.WIDE ? this.getAdaptedValue(TouchpadKbStyle.WIDE_DB_TP_HEIGHT) : this.getAdaptedValue(TouchpadKbStyle.NARROW_KB_TP_HEIGHT)) .margin({ top: this.getAdaptedValue(16) }) .overlay(this.overlayImage(), { align: Alignment.Top }) } } 逐行分析该段代码作用
最新发布
07-04
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值