更改活动时触发onTouch时app会损坏

时间:2017-03-09 13:55:14

标签: android surfaceview ontouchevent ontouch

我做了一个小游戏触摸屏幕向上移动,释放下去。但是,玩家可能会因屏幕边界冲突而死亡。碰撞时,一项新活动开始。 “不幸的是应用程序停止了”,当我在碰撞后继续按下时出现。使用import UIKit class OptionsViewAnimator: NSObject, UIViewControllerAnimatedTransitioning { var type : UINavigationControllerOperation let duration = 0.35 init(operator type:UINavigationControllerOperation) { self.type = type } func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval { return self.duration } func animateTransition(using transitionContext: UIViewControllerContextTransitioning) { switch type { case .push: showOptions(using: transitionContext) case .pop: hideOptions(using: transitionContext) default: break } } func showOptions(using context: UIViewControllerContextTransitioning) { let src = context.viewController(forKey: .from)! let srcView = context.view(forKey: .from)! let dstView = context.view(forKey: .to)! let nav = src.navigationController let toolbar = nav?.toolbar let screen = UIScreen.main.bounds let origin = srcView.frame.origin var dstSize = srcView.frame.size dstSize.height = screen.height - origin.y dstView.frame = CGRect(origin: origin, size: dstSize) dstView.layer.position = origin dstView.layer.anchorPoint = CGPoint(x:0.0,y:0.0) dstView.transform = dstView.transform.scaledBy(x: 1.0, y: 0.01) let container = context.containerView container.addSubview(dstView) srcView.window?.backgroundColor = toolbar?.barTintColor nav?.setToolbarHidden(true, animated: false) UIView.animate(withDuration: self.duration, animations: { dstView.transform = .identity toolbar?.alpha = 0.0 } ) { (finished)->Void in context.completeTransition( !context.transitionWasCancelled ) } } func hideOptions(using context: UIViewControllerContextTransitioning) { let src = context.viewController(forKey: .from)! let srcView = context.view(forKey: .from)! let dstView = context.view(forKey: .to)! let screen = UIScreen.main.bounds let origin = srcView.frame.origin var dstSize = srcView.frame.size let nav = src.navigationController let toolbar = nav?.toolbar let barHeight = toolbar?.frame.height ?? 0.0 srcView.layer.anchorPoint = CGPoint(x:0.0,y:0.0) dstSize.height = screen.height - (origin.y + barHeight) dstView.frame = CGRect(origin:origin, size:dstSize) let container = context.containerView container.addSubview(dstView) container.addSubview(srcView) nav?.setToolbarHidden(false, animated: false) toolbar?.alpha = 0.0 UIView.animate(withDuration: 0.35, animations: { srcView.transform = srcView.transform.scaledBy(x: 1.0, y: 0.01) toolbar?.alpha = 1.0 } ) { (finished)->Void in context.completeTransition( !context.transitionWasCancelled ) } } }

我该怎么办?

我正在使用Eclipse,并感谢每个答案

1 个答案:

答案 0 :(得分:0)

查看为响应onTouch事件而执行的代码。如果活动被销毁或已被销毁,它可以执行,例如你使用后台线程或AsyncTask?您需要尊重活动生命周期,否则您可能正在尝试访问不再处于活动状态的活动。