在 else if 条件中迭代变量?
我正在尝试创建一个 if 语句,其条件取决于变量对象。我该怎么办?
这是我想做的事情的要点...
if ([gestureRecognizer state] == UIGestureRecognizerStateEnded)
{
if (somecondition)
{
//Do stuff
}
else if (CGRectIntersectsRect(currentImage.frame, **anyOfTheImagesInMyArray**.frame))
{
//Do stuff
}
}
如何在测试 true 或 false 之前让 else if 条件检查多个 UIView 框架?
更新的代码:
在这种情况下我该怎么办?
if ([gestureRecognizer state] == UIGestureRecognizerStateEnded)
{
if (CGRectIntersectsRect(currentImage.frame, **anyOfTheImagesInMyArray_1**.frame))
{
//Do stuff
}
else if (CGRectIntersectsRect(currentImage.frame, **anyOfTheImagesInMyArray_2**.frame))
{
if (condition) {
//do stuff
}
else if (condition){
//do something else
}
}
else if (CGRectIntersectsRect(currentImage.frame, **anyOfTheImagesInMyArray_3**.frame))
{
if (condition) {
//do stuff
}
else if (condition){
//do something else
}
}
else if (CGRectIntersectsRect(currentImage.frame, **anyOfTheImagesInMyArray_4**.frame))
{
if (condition) {
//do stuff
}
else if (condition){
//do something else
}
}
}
I'm trying to create an if statement with the condition depending on a variable object. How can I go about this?
Here is the gist of what I'm trying to do...
if ([gestureRecognizer state] == UIGestureRecognizerStateEnded)
{
if (somecondition)
{
//Do stuff
}
else if (CGRectIntersectsRect(currentImage.frame, **anyOfTheImagesInMyArray**.frame))
{
//Do stuff
}
}
How can I get the else if condition to check more than one UIView frame before testing true or false?
Updated Code:
What do I do in this case?
if ([gestureRecognizer state] == UIGestureRecognizerStateEnded)
{
if (CGRectIntersectsRect(currentImage.frame, **anyOfTheImagesInMyArray_1**.frame))
{
//Do stuff
}
else if (CGRectIntersectsRect(currentImage.frame, **anyOfTheImagesInMyArray_2**.frame))
{
if (condition) {
//do stuff
}
else if (condition){
//do something else
}
}
else if (CGRectIntersectsRect(currentImage.frame, **anyOfTheImagesInMyArray_3**.frame))
{
if (condition) {
//do stuff
}
else if (condition){
//do something else
}
}
else if (CGRectIntersectsRect(currentImage.frame, **anyOfTheImagesInMyArray_4**.frame))
{
if (condition) {
//do stuff
}
else if (condition){
//do something else
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不确定我的问题是否正确,但如果您想对数组中的每个图像执行一些任务,它应该如下所示:
Not sure if I got the question right, but if you want to perform some tasks on every image in the array it should look e.g. like this:
据我所知,没有办法简洁地使用现有语法来完成您想要做的事情。相反,请考虑对
NSArray
进行分类,添加一个方法,例如-containsIntersectingRect:(CGRect)rect
,该方法循环遍历NSValue
对象数组 - 数组只能包含对象,因此您必须将CGRect
插入到包装为NSValue
对象的数组中,并查找至少一种肯定的情况CGRectIntersectsRect
。这至少会使语法更加集中并抽象为用于一系列条件的单个方法。
To my knowledge, there is no way to do what you're trying to do with existing syntax succinctly. Instead, consider categorizing
NSArray
to add a method such as-containsIntersectingRect:(CGRect)rect
, which loops through the array ofNSValue
objects—arrays can only contain objects, so you will have to insert yourCGRect
s into the array wrapped asNSValue
objects—and looks for at least one positive case forCGRectIntersectsRect
.This will at least make the syntax more collected and abstracted into a single method for your series of conditionals.