UIActivityIndicatorView,在图像下载期间显示在当前视图上
我有一个按钮,按下时,它会从网络下载多个图像,以便在另一个视图中显示它们。然而,目前,当我按下按钮时,按钮会进入突出显示状态,并且在图像下载时似乎卡在那里(基本上准备好下一个视图)。我并不担心按钮停留在突出显示模式(事实上我更喜欢这种方式)。
但是,我想要的是 UIActivityView 显示在显示按钮的视图上,同时加载下一个视图(并且图像是从网络下载的,因为这需要一段时间)...我该怎么办逻辑上执行这个?
谢谢,
杰克
I have a button and, when pressed, it downloads several images from the web in order to display them in another view. However, at the moment, when I press the button, the button goes to its highlighted state and seems to get stuck there while the images download (and essentially the next view is prepared). I'm not fussed about the button being stuck in highlighted mode (in fact I prefer it that way).
However, what I would like is for a UIActivityView to be displayed on the view where the button is displayed while the next view loads (and the images are downloaded from the web, as this takes a while)...How would I go about implementing this logically?
Thanks,
Jack
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我假设您正在使用 NSURLConnection 来连接和下载图像?如果是这种情况,您可能需要使用如下方法:
在 IB 中,将 UIActivityIndicatorView 放置在视图上您想要的位置,并在其属性面板中定义其样式。告诉它停止时隐藏。并确保将其与所有者头文件中的代码链接。
当
您创建 NSURLConnection 并开始请求时,请使用
[indicator startAnimation]; 告诉指示器开始动画。如果您不想让按钮出现在屏幕上,您可以根据需要将其从超级视图中删除。
下载完成后,您可以告诉
- (void)connectionDidFinishLoading:(NSURLConnection * )连接
方法。如果您在不使用 NSURLConnection 的情况下下载图像,这当然会有所不同,但从逻辑上讲它应该是相同的方法。虽然我同意大卫的观点,这可能应该在后台线程上完成
I assume you're using NSURLConnection to connect and download the images? If this is the case, you would want to use an approach like this:
In IB, place a UIActivityIndicatorView on your view where you'd like it and define its style in its Attributes panel. Tell it to Hide When Stopped. And be sure to link it with your code in the owner's header file.
and
When you create your NSURLConnection and begin the request, tell the indicator to begin animation with
[indicator startAnimation];
. If you don't want your button on screen, you can remove it from the superview if you like.Once you're done downloading, you can tell the indicator
[indicator stopAnimation]
(and add your button back if you had removed it earlier) in your- (void)connectionDidFinishLoading:(NSURLConnection *)connection
method.If you are downloading the images without using NSURLConnection, this of course would be different, but logically it should be the same approach. Though I agree with David that this should probably be done on a background thread
您正在主线程中进行长时间的工作。您应该更改代码,以便下载在后台线程中进行。您可以更改按钮的状态以指示下载正在进行中,但不要以这种方式阻止主线程。
在后台线程中,加载图片并调用主线程上的选择器,该选择器更新 UIActivtyView 以在下载更多图片时指示其进度。
You are doing long work in the main thread. You should change your code so the download occurs in a background thread. You can change the state of the button to indicate the download is in progress, but don't holdup the main thread this way.
In the background thread, load the pictures and call a selector on the main thread which updates the UIActivtyView to indicate its progress as it downloads more pictures.
对于您的按钮问题,为突出显示状态分配与为正常分配的图像相同的图像
添加活动指示器
对于在 .h 文件中
,请在 xib 中添加属性
并进行连接。
在您的按钮上合成并释放 in.m 文件,
单击您想要显示指标的位置:-
您想要停止指标写入的位置
For your button problem assign the same image for highlighted state that you are assigning for normal
For adding activity indicator
in .h file
make property
add indicator in your xib and do connection.
synthesize and release in.m file
on your button click where you want to show your indicator:-
where you want to stop your indicator write
您可以将指标框架设置在您需要的地方; UIButton 的中心。
您尚未指定如何下载图像,但开始下载时,请使用
[indicator startActivity];
,下载完成后,请使用[indicator stopActivity];
您还可以在添加开始/停止调用的位置添加
setHidden:YES
/setHidden:NO
。You would set your indicators frame to be where you need it; center of the UIButton.
You haven't specified how you download your images, but when you start the download, use
[indicator startActivity];
and when the download is complete, use[indicator stopActivity];
You can also add a
setHidden:YES
/setHidden:NO
where you add the start/stop calls.