0

I have a parent viewcontroller , and there have a subview(UIView).

The subview(named xibSubView) is called other custom class(xib) file.

When I click on the parent Viewcontroller, the subview(UIView) will motion to touch began position on the parent viewcontroller.

The parent viewcontroller have

 // UIViewcontroller.h file
  .....    
 @property (weak, nonatomic) IBOutlet XibSubView *xibSubView;
 .....

The below code is in the parent viewcontroller .m

 -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
 {
    self.subViewConstraint.constant = touchLeftScreenPoint.x ;
    self.subViewConstraint.constant = touchLeftScreenPoint.y ;

 }

 -(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
 {
 }

 -(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
 {
 }

And the xib(subview-xibSubView) code have the same delegate method.

 // The code is in the parent viewcontroller .m
 -(void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
 {
 }

 -(void) touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event
 {
 }

 -(void) touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
 {
 }

In the xibSubView have much elements.

One of the elements will motion to the click position in the subview(xibSubView).

So I want to pass the parent viewcontroller touches into xibSubView.

And manual call the touchBegan in the xibSubView let the element motion.

So I should transform the parent viewcontroller touches position to subview touches position.

The subview touches x position and y position will subtract the viewcontroller width and height and manual call the touchbegan method.

But I don't know how to changed the touches x, y values restore to touches.

Then call the code:

 [self.xibSubView touchesBegan:touches withEvent:event];

How can I change the NSSet touches x,y variable and restore to touches.

Thank you very much.

dickfala
  • 3,246
  • 3
  • 31
  • 52
  • Doing exactly this is hard. When redirecting events, it is usually done from subviews to superview not the other way around. This seems more like an architecture problem though. Usually you want only one view to handle touches and if you want to pass events to subviews, use custom methods (e.g. passing only the position). It will make the problem easier. – Sulthan May 07 '15 at 09:07
  • in fact, the subview is like joystick, so that have move self from user click subview. and I will click on the parent viewcontroller, the joystick will change the self view to touch postion, and It can move immediately. – dickfala May 07 '15 at 09:37
  • That doesn't change my comment. – Sulthan May 07 '15 at 09:39

2 Answers2

5
NSMutableArray *touchArray = [NSMutableArray new];
[touchArray addObject:[UITouch new]];
NSSet *touches = [[NSSet alloc] init];
[touches setByAddingObjectsFromArray:touchArray];
[self touchesBegan:touches withEvent:UIEventTypeTouches];

Swift

let tArr = NSMutableArray()
tArr.add(UITouch())
let touch = NSSet()
touch.adding(tArr)

touchesBegan(touch as! Set<UITouch>, with: nil)
Fawad Masud
  • 12,219
  • 3
  • 25
  • 34
Britto Thomas
  • 2,092
  • 1
  • 15
  • 28
  • I have to transform the coordinates. The touches x, y in the parent viewcontroller is different with subview. So how can I transform to touches value and restored to the NSSet, then pass to the touchesBegan. – dickfala May 07 '15 at 09:05
  • cant create a uitouch object programatically. – Britto Thomas May 07 '15 at 09:07
  • I know we can't create a uitouch object programmatically, so I want to get the touch value from the parent viewcontroller from user touch on the parent viewcontroller, then get the touches value to subtract some value , pass into the xibsubview touches Began.So How can I change the touches value and restore?thank you. – dickfala May 07 '15 at 09:12
  • the xibSubView extends UIView,It's xib file.It was called about touch began when I touch on the subview.@@ – dickfala May 07 '15 at 09:28
  • suppose touch x position is substract parent viewcontroller x position.How can I change the touches x value pass into the subview touchbegan. I know how to called subview touchbegan, but I don't know how to change the touches CGpoint value. – dickfala May 07 '15 at 09:31
1

ViewController.m

#import "ViewController.h"
#import "view.h"

@interface ViewController ()
{
 view *v;
}
@end

@implementation ViewController
- (void)viewDidLoad {
   v=[[view alloc]initWithFrame:CGRectMake(10, 10, 100, 100)];
   [self.view addSubview:v];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
   [v touchesBegan:touches withEvent:event];
}
@end

view.h

#import <UIKit/UIKit.h>

@interface view : UIView
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event;
@end

view.m

#import "view.h"

@implementation view

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
  NSLog(@"%@",touches);
}
@end
Britto Thomas
  • 2,092
  • 1
  • 15
  • 28
  • 1
    this will trigger touch action in UIViewController to UIView. – Britto Thomas May 07 '15 at 09:40
  • or you can do the reverse, touch in UIView to UIViewController – Britto Thomas May 07 '15 at 09:40
  • My view coordinate was not same with parent viewcontroller, So in your case, ViewController's touchbegan method get touches variable will transform x,y coordinate(like substract touchesPoint.x ), then call [v touchesBegan:touches withEvent:event]; The problem is on I don't know when I transform the some value, How can I save to touches variable, then call [v touchesBegan:touches withEvent:event]; Thank you – dickfala May 07 '15 at 09:42
  • http://stackoverflow.com/questions/14141846/how-to-change-a-uitouchs-location-before-passing-to-the-next-responder – Britto Thomas May 07 '15 at 09:45
  • donot make conversions to uitouch, refer above link – Britto Thomas May 07 '15 at 09:48
  • But my case have move the subview to parent view touch position.and in the subview elements have to subtract offset value.because the [touch locationInView:view] the view is parent view not subview. @@ – dickfala May 07 '15 at 10:04
  • I got it!!! I change to [touch locationInView:self]. the coordinate is correct! thank you very much,@Britto Thomas. – dickfala May 07 '15 at 10:11