如何创建一个while循环,每秒检查一次?
可能是这样的:
while (isConditionSatisfied){
// wait for 1 second and than check again
}
编辑:系统随机调用此函数bannerViewDidLoadAd
。如果它在不适当的时间调用它(条件不满意 - 我的应用程序正在执行其他动画),我想推迟其实现(只是一个UIView动画),直到满足条件(我的应用程序已完成动画,现在实现应该被执行)。我以为我可以每秒钟检查一下这个状态,但正如你们所说的那样......这是一个坏主意。
答案 0 :(得分:9)
像这样使用while循环实际上会创建一个无限循环。你应该使用Timer()。
package com.lightelements.carddroid;
import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.List;
/**
* Created by User on 11/09/2015.
*/
public class CardsCustomAdapter extends ArrayAdapter<Card> {
private LayoutInflater mLayoutInflater;
private static FragmentManager sFragmentManager;
public CardsCustomAdapter(Context context, FragmentManager fragmentManager){
super(context, android.R.layout.simple_list_item_2);
mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
sFragmentManager = fragmentManager;
}
public CardsCustomAdapter(Context context){
super(context, android.R.layout.simple_list_item_2);
mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final View view;
if (convertView == null) {
view = mLayoutInflater.inflate(R.layout.custom_card, parent, false);
} else {
view = convertView;
}
final Card card = getItem(position);
final int _id = card.getId();
final String name = card.getCard_name();
((TextView) view.findViewById(R.id.card_name)).setText(name);
return view;
}
public void setData(List<Card> cards){
clear();
if (cards != null) {
for (Card card : cards) {
add(card);
}
}
}
}
然后
timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(self.loop), userInfo: nil, repeats: true)
确保使用其他变量声明声明计时器,以便在满足条件后它可以失效:
@objc
func loop() {
if yourCondition {
// your code here
timer.invalidate()
}
}
答案 1 :(得分:1)
你可以像这样使用NSTimer
[NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(yourMethodName) userInfo:nil repeats:YES];
答案 2 :(得分:1)
我刚刚整理了一个Swift NSObject扩展,允许你这样做。它使用延迟功能,允许您wait
直到满足某些条件或取消它。
https://gist.github.com/d3signerd/795a9dced39cbee056010d5629d9ca06
//等待示例
waitWhile( { [weak self] () -> condition_parameters in
if let weakSelf = self {
return ( conditionMet: `criteria to meet`, cancel: false )
}
else {
return ( conditionMet: false, cancel: true )
}
}, completion: { [weak self] in
// Wait completion code goes here...
})
//延迟示例
private var cancelableDelayClosure:dispatch_cancelable_closure? = nil
cancelableDelayClosure = delay( 1.0, closure: { () -> () in
// Delay completion code here...
})
cancelDelay( &cancelableDelayClosure )
答案 3 :(得分:0)
更新了swift的代码,
let timer = Timer.scheduledTimer(
timeInterval: 1.0, target: self, selector: #selector(Your_current_vc.yourTask),
userInfo: nil, repeats: true)
func yourTask() {
}
答案 4 :(得分:0)
iOS 10
Timer.scheduledTimer(withTimeInterval: 1, repeats: true) { (timer) in
if someCondition {
timer.invalidate()
}
}