Xcode 7:有没有办法使所有计时器无效?

时间:2016-01-03 18:48:53

标签: nstimer invalidation

我正在制作一个游戏,您可以在第2阶段进行游戏,然后跳回到第1阶段。但游戏会与计时器一起运行。有没有办法使所有NSTimers无效?

喜欢(刚做完):

public class BinderData_TvGids extends BaseAdapter {
    LayoutInflater inflater;
    List<HashMap<String,String>> TvGids;
    ViewHolder holder;

    public BinderData_TvGids() {}

    public BinderData_TvGids(Activity act, List<HashMap<String, String>> map) {
        this.TvGids = map;
        inflater = (LayoutInflater) act.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
        return TvGids.size();
    }

    public Object getItem(int arg0) {
        return null;
    }

    public long getItemId(int position) {
        return 0;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        File ext = Environment.getExternalStorageDirectory();
        File cacheDir = new File(ext, "/android/data/apk/case");
        AQUtility.setCacheDir(cacheDir);
        AQuery aq = new AQuery(convertView);

        ViewHolder holder = null;
        View vi=convertView;
        if(TvGids.get(position).get("header").equals("true")) { //headers worden meegegeven uit array, deze krijgen een apparten layout
            if (convertView == null) {
                vi = inflater.inflate(R.layout.list_row_tvgids_header, null);
                holder = new ViewHolder();
                holder.titel = (TextView) vi.findViewById(R.id.titel); // naam
                vi.setTag(holder);
            } else {
                holder = (ViewHolder) vi.getTag();
            }
            if (holder.titel != null)
                holder.titel.setText(TvGids.get(position).get("datum")); // naam
            return vi;
        } else {
            if (convertView == null) {
                vi = inflater.inflate(R.layout.list_row_tvgids, null);
                holder = new ViewHolder();
                holder.poster =(ImageView)vi.findViewById(R.id.poster); // poster
                holder.zenderlogo =(ImageView)vi.findViewById(R.id.ZenderLogo); // poster
                holder.titel = (TextView) vi.findViewById(R.id.titel); // naam
                holder.starttijd = (TextView) vi.findViewById(R.id.starttijd); // naam
                holder.loader =(ProgressBar)vi.findViewById(R.id.progress); // progressbar
                vi.setTag(holder);
            } else {
                holder = (ViewHolder) vi.getTag();
            }

            if (holder.poster != null & holder.loader != null)
                    if(!TvGids.get(position).get("film_key").contains("TV") && !TvGids.get(position).get("film_key").contains("BS")) {
                        holder.poster.setVisibility(View.INVISIBLE);
                        aq.id(holder.poster).progress(holder.loader).image("http://cdn.be/hosting/movie/" + TvGids.get(position).get("film_key") + "/null/200/poster.jpg", false, true, 0, R.drawable.poster_x, null, AQuery.FADE_IN_NETWORK);
                    } else {
                        holder.loader.setVisibility(View.INVISIBLE);
                    }
            if (holder.zenderlogo != null)
                aq.id(holder.zenderlogo).image("http://cdn.be/hosting/zender/rw.php?zender=" + TvGids.get(position).get("zender") + "&size=166&opacity=50", false, true, 0, 0, null, AQuery.FADE_IN);
            if (holder.starttijd != null)
                holder.starttijd.setText(TvGids.get(position).get("starttijd")); // naam
            if (holder.titel != null)
                holder.titel.setText(TvGids.get(position).get("titel")); // naam
            return vi;
        }
    }

    static class ViewHolder{
        TextView starttijd;
        TextView titel;
        ImageView poster;
        ImageView zenderlogo;
        ProgressBar loader;
    }
}

2 个答案:

答案 0 :(得分:3)

没有方法可以使所有计时器无效。您需要将计时器引用存储在一个数组中,然后您可以进行快速枚举并使它们全部无效。

例如:

for (id timerObject in timerObjectsArray) {
    if ([timerObject isValid]) {
        [timerObject invalidate];
    }
}

答案 1 :(得分:0)

没有这样的方法。如果有,它可能会破坏很多东西,因为你的代码不是唯一可以使用计时器的代码。框架也可以。

但是,您可以保留游戏正在使用的所有计时器的NSMutableArray,并使阵列中的所有计时器无效,如下所示:

//whenever you create a timer:
[timers addObject:timer];    //timers is the NSMutableArray of all your timers
//when you invalidate a timer:
[timers removeObject:timer];
//to remove all timers:
[timers makeObjectsPerformSelector:@selector(invalidate)]
[timers removeAllObjects];

注意:我没有测试过这段代码,如果你把重复的计时器放在数组中,它可能会引起问题。