質問

I want to find all subviews which are inside of a particular frame size.

I have my custom UITableViewCell, in that cell I am attaching various UIImageViews as subviews of UITableViewCell content view, and I added UILongPressGestureRecognizer in each cell.

My action method of UILongPressGestureRecognizer looks like this:

- (void)handleLongPressGestureRecgonizer:(UILongPressGestureRecognizer *)sender
{
    if(sender.state == UIGestureRecognizerStateBegan)
    {
        self.startPoint = CGPointMake([sender locationInView:sender.view].x,
                             [sender locationInView:sender.view].y);

    }
    if(sender.state == UIGestureRecognizerStateEnded)
    {
        self.endPoint = CGPointMake([sender locationInView:sender.view].x,
                           [sender locationInView:sender.view].y);
        CGRect frame = CGRectMake(self.startPoint.x, self.bounds.origin.y, self.endPoint-self.startPoint, self.bounds.size.height);

    }
 }

Now I want to find out what are the UIImageView located inside the frame that I found in action method of UILongPressGestureRecognizer.

But I am thinking that I have to iterate all the cell contentView subview and check if that frame intersects with my identified frame or not.

Either my approach is correct or there is any other easy way to achieve this, kindly help me.

Thank you...

役に立ちましたか?

解決

If you want to find out what views are in a specified section of a common superview then you do need to do some iteration and a 'hit detection'. Using the frame intersection as you say is good.

Rather than iterating all subviews, which would need to be recursive, you should maintain a property which is an array of all of the image views such that you can iterate that array directly and ignore any other subviews that aren't interesting.

他のヒント

You should set tag for UIImageViews which are subview of UITableViewCell

You can get indexPath of row :

-(void)handleLongPressGestureRecgonizer:(UILongPressGestureRecognizer *)sender{  
  CGPoint p = [sender locationInView:self.moTableView];
  NSIndexPath *indexPath = [self.moTableView indexPathForRowAtPoint:p];
  //get your cell, get all images in your cell
 }

you can use clean code with objective c run time like this: in header file declare:

extern const char* MyConstantKey;

@interface .....yourInterface

and in .m you can define like this: //private stuffs goes here const char* MyConstantKey = "MyConstantKey";

then some thing like this:

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(longPress:)];
objc_setAssociatedObject(longPress,
                             (void*)MyConstantKey,
                             yourArrayOfImageViews,
                             OBJC_ASSOCIATION_RETAIN);

and you can can associated object like this:

- (void)longPress:(UILongPressGestureRecognizer*)gesture
{
    NSArray *arrayOfImages = objc_getAssociatedObject(gesture, (void*)MyConstantKey);
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top