index.uts 2.5 KB
Newer Older
1 2
import { UIImageView , UIImage , UIScreen } from 'UIKit';
import { DispatchQueue } from 'Dispatch';
lizhongyi_'s avatar
lizhongyi_ 已提交
3
import { CGFloat } from 'CoreFoundation';
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
export function addViewToDecorView() { }
export function removeViewToDecorView() { }
export function initAppLifecycle() { }
export function getLogoPath() {}
export function playAssetAudio(){}

/**
 * 定时任务参数封装
 */
type TimerOptions = {
	/**
	 * 定时任务开始的回调
	 * @res 回调参数
	 */
	start: (res: string) => void;
	/**
	* 定时任务执行的回调
	* @res 回调参数
	*/
	work: (res: string) => void;
};


/**
 * 执行延时任务
 */
export function doTimerTask(opts:TimerOptions) {
	opts.start('doTimerTask start');
	setTimeout(function() {
		opts.work("doTimerTask work");
	}, 2000);
  
  return { name: "doTimerTask" };
}

/**
 * 执行周期任务
 */
export function doIntervalTask(opts:TimerOptions) {
	
	let taskRet = setInterval(function() {
		opts.work("doIntervalTask work");
	}, 2000);
	opts.start('doIntervalTask start');
  
  return { name: "doIntervalTask",taskId:taskRet};
}

/**
 * 清除周期任务
 */
export function clearIntervalTask(taskId:number) {
	
	clearInterval(taskId);
	return { name: "clearIntervalTask"};
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
}

/* 
 * 保存全局数据信息
 */
class AdvanceModuleGloabInfo {
	static imageView?: UIImageView = null
}


/**
 * 将h5资源路径转成app本地资源路径 
 */
export function getResourcePath(path: string) {
	const imagePath = UTSiOS.getResourcePath(path)
	console.log(imagePath)
	
	if (AdvanceModuleGloabInfo.imageView == null) { 
		let vc = UTSiOS.getCurrentViewController()
		// uts方法默认会在子线程中执行,涉及 UI 操作必须在主线程中运行,通过 DispatchQueue.main.async 方法可将代码在主线程中运行
		DispatchQueue.main.async(execute=():void => {
			// 创建imageView
			let imageView = new UIImageView()
			let image = new UIImage(contentsOfFile = imagePath)
			imageView.image = image
			
			// 添加imageView并设置frame
			vc.view.addSubview(imageView)
lizhongyi_'s avatar
lizhongyi_ 已提交
87
			let imageSize: CGFloat = 80.0
88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104
			let midx = (UIScreen.main.bounds.size.width - imageSize) / 2
			let midy = (UIScreen.main.bounds.size.height - imageSize) / 2
			imageView.frame = CGRect(x = midx, y = midy, width = imageSize, height = imageSize)
			AdvanceModuleGloabInfo.imageView = imageView
		})
	}

}


export function removeExampleImageView() {
	DispatchQueue.main.async(execute=():void => {
		if (AdvanceModuleGloabInfo.imageView != null) {
			AdvanceModuleGloabInfo.imageView!.removeFromSuperview()
			AdvanceModuleGloabInfo.imageView = null
		}
	})
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
105
}