我想在 SwiftUI 视图中通过 CocoaPods使用这个项目Toast-Swift 。它是针对 UIView 的,所以我尝试编写一个 ViewController 并将其包装到 SwiftUI 中,但结果屏幕上什么也没有。
我的代码:
struct ToastView: UIViewControllerRepresentable{
@State var text: String
func makeUIViewController(context: UIViewControllerRepresentableContext<ToastView>) -> UIToast {
return UIToast(text: text)
}
func updateUIViewController(_ uiViewController: UIToast, context: UIViewControllerRepresentableContext<ToastView>) {
}
}
class UIToast: UIViewController{
var text: String = ""
init(text: String) {
super.init(nibName: nil, bundle: nil)
self.text = text
}
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
self.view.makeToast(text)
}
}
Run Code Online (Sandbox Code Playgroud)
我在 SwiftUI 的 …
我知道onLongTouchListener不存在,所以我正在尝试制作自己的版本,因为我真的需要触摸坐标,所以不能使用onLongClickListener。我的代码如下:
touchImageView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, final MotionEvent motionEvent) {
CountDownTimer counter = new CountDownTimer(500, 1) {
public void onTick(long millisUntilFinished) {
System.out.println("REMAINING: "+(millisUntilFinished));
}
public void onFinish() {
System.out.println("IT'S OVER");
}
};
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN){
counter.start();
} else if( motionEvent.getAction() == MotionEvent.ACTION_UP){
counter.cancel();
}
return true;
}
});
Run Code Online (Sandbox Code Playgroud)
问题是:当我将手指放在屏幕上时,计数器开始计数,但是按我的预期抬起手指并没有取消计数:
else if( motionEvent.getAction() == MotionEvent.ACTION_UP){
counter.cancel();
}
Run Code Online (Sandbox Code Playgroud)
你们能帮我吗?
编辑:
遵循评论和答案后,它现在可以工作:
final CountDownTimer counter;
counter = new CountDownTimer(500,1) {
@Override
public void onTick(long l) {
System.out.println("REMAINING "+l); …Run Code Online (Sandbox Code Playgroud)