Michael Yuan
Michael Yuan

Reputation: 73

How to send touch event to a UIView on iOS

I have added a tap gesture on a view. When the view tapped, an internal method called and an alert popped up.

Now I want to send touch event to simulate finger touch on this view. How could I do this?

===============

Thanks all.

I think I should describe question clearly. I'm work on ReplayKit on iOS12. We can add RPSystemBroadcastPickerView to our custom view. In my project, click a button, RPSystemBroadcastPickerView init and add in my view, tap on RPSystemBroadcastPickerView, select items, and click Start Broadcast.

What I want to do is that When I click button, it jump to the select items interface automatically. Just like sending touch event to RPSystemBroadcastPickerView after button clicked.

Upvotes: 2

Views: 5321

Answers (4)

Muhammad Ehsan Mirzaei
Muhammad Ehsan Mirzaei

Reputation: 129

first, create a RPSystemBroadcastPickerView in a position of the screen and without height and width to don't show its icon and add it to you controller(for me topController) then select most top view of RPSystemBroadcastPickerView that is button then call its action -> theBroadcastPicker should open now without any click on screen

 var  broadCastPicker = RPSystemBroadcastPickerView(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
topController.view.addSubview(broadCastPicker!)
var tap = broadCastPicker?.subviews.first as! UIButton
tap.sendActions(for: .touchUpInside)

Upvotes: 1

Hamidreza Vakilian
Hamidreza Vakilian

Reputation: 860

This might help you. But keep in mind that the touching mechanism in iOS has a complex structure and this block of code might not result in what you're looking for.

targetView.touchesBegan(.init(), with: nil)
targetView.touchesEnded(.init(), with: nil)

Upvotes: 0

Margin
Margin

Reputation: 56

UIButton *btnInPicker = nil;
for( UIView *_subView in self.broadcastPicker.subviews) {
    if( [_subView isKindOfClass:[UIButton class]] ) {
        btnInPicker = (UIButton *)_subView;
        break;
    }
}


if(btnInPicker) {
    [btnInPicker sendActionsForControlEvents:UIControlEventTouchDown];
}

but it has a issue: Unbalanced calls to begin/end appearance transitions for

Upvotes: 2

Mahendra
Mahendra

Reputation: 8914

You can create a custom view and override following methods...

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event;
- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event;
- (void)touchesMoved:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event;
- (void)touchesCancelled:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event;

Swift Version

func touchesBegan(Set<UITouch>, with: UIEvent?)

Tells this object that one or more new touches occurred in a view or window.


func touchesMoved(Set<UITouch>, with: UIEvent?)

Tells the responder when one or more touches associated with an event changed.


func touchesEnded(Set<UITouch>, with: UIEvent?)

Tells the responder when one or more fingers are raised from a view or window.


func touchesCancelled(Set<UITouch>, with: UIEvent?)

Tells the responder when a system event (such as a system alert) cancels a touch sequence.

For More details you can refer this link.

Upvotes: 0

Related Questions