我想在我的应用程序运行时调暗手机屏幕,如果在一段时间内(例如10秒)没有触摸事件,则在屏幕上的任何地方再次触摸时使屏幕更亮。< / p>
搜索SO后,似乎我需要创建自定义<section id="content">
<div class="container" data-ng-controller="tableUserCtrl">
<div class="p-t-0" data-ng-controller="ModalDemoCtrl">
<script type="text/ng-template" id="adduser.html">
<div class="modal-header">
<!--<h4 class="modal-title">Add User</h4>-->
</div>
<form role="form" ng-submit="insertInfo(userInfo);" name="userForm" novalidate>
...
<div class="modal-footer">
<button class="btn btn-link" ng-click="ok(); $parent.addUserT(user);" ng-disabled="userForm.$invalid">Submit</button>
<button class="btn btn-link" ng-click="cancel()">Cancel</button>
</div>
</form>
</script>
<button class="btn btn-default pull-right" ng-click="openStatic('adduser')">Add User</button><br/><br/>
</div>
<div class="card">
<div class="card-header">
<h2>Users <small></small></h2>
</div>
<div class="card-body">
<div class="table-responsive">
<table ng-table="tableEdit" class="table table-striped table-vmiddle" show-filter="true">
<tr ng-repeat="w in $data" ng-class="{ 'active': w.$edit }">
<td data-title="'ID'" filter="{ 'role_no': 'text' }" sortable="'role_no'">
<span ng-if="!w.$edit">{{ w.role_no }}</span>
<div ng-if="w.$edit"><input class="form-control" type="text" ng-model="w.role_no" /></div>
</td>
...
<td data-title="'Actions'">
<button type="button" class="btn btn-default" ng-if="!w.$edit" ng-click="w.$edit = true"><i class="zmdi zmdi-edit"></i></button>
<button type="button" class="btn btn-default" ng-if="!w.$edit" ng-click="removeUser(w.user_id, w)"><i class="zmdi zmdi-close"></i></button>
<button type="button" class="btn btn-success" ng-if="w.$edit" ng-click="w.$edit = false; updateUser(w)"><i class="zmdi zmdi-check"></i></button>
</td>
</tr>
</table>
</div>
</div>
</div>
</div>
</section>
才能处理所有触摸。以下是我目前的代码:
UIApplication
打印输出看起来像这样:
import UIKit
@objc(MyApplication)
class MyApplication: UIApplication {
override func sendEvent(_ event: UIEvent) {
var screenUnTouchedTimer = Timer.scheduledTimer(timeInterval: 10, target: self, selector: #selector(self.makeScreenDim), userInfo: nil, repeats: true);
// Ignore .Motion and .RemoteControl event simply everything else then .Touches
if event.type != .touches {
super.sendEvent(event)
return
}
// .Touches only
var restartTimer = true
if let touches = event.allTouches {
// At least one touch in progress? Do not restart timer, just invalidate it
self.makeScreenBright()
for touch in touches.enumerated() {
if touch.element.phase != .cancelled && touch.element.phase != .ended {
restartTimer = false
break
}
}
}
if restartTimer {
// Touches ended || cancelled, restart timer
print("Touches ended. Restart timer")
} else {
// Touches in progress - !ended, !cancelled, just invalidate it
print("Touches in progress. Invalidate timer")
}
super.sendEvent(event)
}
func makeScreenDim() {
UIScreen.main.brightness = CGFloat(0.1)
print("makeScreenDim")
}
func makeScreenBright() {
UIScreen.main.brightness = CGFloat(0.5)
print("makeScreenBright")
}
}
正如您在上面看到的那样,代码存在很大问题,似乎我正在为每个触摸事件创建一个新的Timer。我不知道如何在UIApplication中创建一个静态(只有一个)Timer。
我应该如何以正确的方式只实现一个计时器?
(我正在使用Iphone7,最新版本的swift和xcode)
答案 0 :(得分:0)
您必须在某处使先前创建的计时器无效,否则您将获得所描述的行为。
在每次调用sendEvent时将其存储在属性中,以便下次调用该方法时可以访问它。
class MyApplication: UIApplication {
var screenUnTouchedTimer : Timer?
override func sendEvent(_ event: UIEvent) {
screenUnTouchedTimer?.invalidate()
screenUnTouchedTimer = Timer ......