iOS-보기를 통해 모든 터치 전달
다른 많은보기 위에 오버레이 된보기가 있습니다. 나는 화면에서 몇 가지 터치를 감지하기 위해 Overaly를 사용하고 있지만 그 외에는 뷰가 스크롤 뷰와 같은 다른 뷰의 동작을 멈추고 싶지 않습니다. 모든 터치를 전달하려면 어떻게해야합니까? 이 오버레이 뷰? UIView의 일부입니다.
오버레이보기에서 아래보기로 터치를 전달하려면 UIView에서 다음 메소드를 구현하십시오.
목표 -C :
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event {
NSLog(@"Passing all touches to the next view (if any), in the view stack.");
return NO;
}
빠른:
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
print("Passing all touches to the next view (if any), in the view stack.")
return false
}
사용자 상호 작용을 비활성화하는 것만으로도 충분했습니다!
목표 -C :
myWebView.userInteractionEnabled = NO;
빠른:
myWebView.isUserInteractionEnabled = false
이것은 오래된 스레드이지만 검색시 나왔으므로 2c를 추가 할 것이라고 생각했습니다. 하위보기가있는 덮는 UIView가 있고 하위보기 중 하나에 닿는 터치 만 가로 채기를 원하므로 PixelCloudSt의 답변을 다음과 같이 수정했습니다.
-(BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
for (UIView* subview in self.subviews ) {
if ( [subview hitTest:[self convertPoint:point toView:subview] withEvent:event] != nil ) {
return YES;
}
}
return NO;
}
@fresidue 답변의 개선 된 버전. 이 UIView
서브 클래스는 서브 뷰 외부로 터치를 전달하는 투명한 뷰로 사용할 수 있습니다 . Objective-C의 구현 :
@interface PassthroughView : UIView
@end
@implementation PassthroughView
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
for (UIView *view in self.subviews) {
if (!view.hidden && [view pointInside:[self convertPoint:point toView:view] withEvent:event]) {
return YES;
}
}
return NO;
}
@end
그리고 스위프트에서 :
class PassthroughView: UIView {
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
return subviews.contains(where: {
!$0.isHidden && $0.point(inside: self.convert(point, to: $0), with: event)
})
}
}
터치를 전달하려는 뷰가 서브 뷰 / 슈퍼 뷰가 아닌 경우 UIView 서브 클래스에서 다음과 같이 사용자 정의 속성을 설정할 수 있습니다.
@interface SomeViewSubclass : UIView {
id forwardableTouchee;
}
@property (retain) id forwardableTouchee;
당신의 .m에서 그것을 합성하십시오 :
@synthesize forwardableTouchee;
그런 다음과 같은 UIResponder 메소드에 다음을 포함하십시오.
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[self.forwardableTouchee touchesBegan:touches withEvent:event];
}
UIView를 인스턴스화 할 때마다 forwardableTouchee 속성을 이벤트를 전달할보기로 설정하십시오.
SomeViewSubclass *view = [[[SomeViewSubclass alloc] initWithFrame:someRect] autorelease];
view.forwardableTouchee = someOtherView;
UIStackView와 비슷한 문제가 있었지만 다른보기 일 수 있습니다. 내 구성은 다음과 같습니다.
It's a classical case where I have a container that needed to be placed in the background, with buttons on the side. For layout purposes, I included the buttons in a UIStackView, but now the middle (empty) part of the stackView intercepts touches :-(
What I did is create a subclass of UIStackView with a property defining the subView that should be touchable. Now, any touch on the side buttons (included in the * viewsWithActiveTouch* array) will be given to the buttons, while any touch on the stackview anywhere else than these views won't be intercepted, and therefore passed to whatever is below the stack view.
/** Subclass of UIStackView that does not accept touches, except for specific subviews given in the viewsWithActiveTouch array */
class NoTouchStackView: UIStackView {
var viewsWithActiveTouch: [UIView]?
override func hitTest(point: CGPoint, withEvent event: UIEvent?) -> UIView? {
if let activeViews = viewsWithActiveTouch {
for view in activeViews {
if CGRectContainsPoint(view.frame, point) {
return view
}
}
}
return nil
}
}
In Swift 5
class ThroughView: UIView {
override func point(inside point: CGPoint, with event: UIEvent?) -> Bool {
guard let slideView = subviews.first else {
return false
}
return slideView.hitTest(convert(point, to: slideView), with: event) != nil
}
}
Try something like this...
for (UIView *view in subviews)
[view touchesBegan:touches withEvent:event];
The code above, in your touchesBegan method for example would pass the touches to all of the subviews of view.
As suggested by @PixelCloudStv if you want to throw touched from one view to another but with some additional control over this process - subclass UIView
//header
@interface TouchView : UIView
@property (assign, nonatomic) CGRect activeRect;
@end
//implementation
#import "TouchView.h"
@implementation TouchView
#pragma mark - Ovverride
- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
BOOL moveTouch = YES;
if (CGRectContainsPoint(self.activeRect, point)) {
moveTouch = NO;
}
return moveTouch;
}
@end
After in interfaceBuilder just set class of View to TouchView and set active rect with your rect. Also u can change and implement other logic.
참고URL : https://stackoverflow.com/questions/3834301/ios-forward-all-touches-through-a-view
'IT박스' 카테고리의 다른 글
지정된 유형 멤버 'Date'는 LINQ to Entities에서 지원되지 않습니다. (0) | 2020.07.09 |
---|---|
Java를 사용하여 이미지 크기를 조정하는 방법 (0) | 2020.07.09 |
Node.js에서 비 차단 또는 비동기 I / O 란 무엇입니까? (0) | 2020.07.08 |
Seaborn 플롯을 파일로 저장하는 방법 (0) | 2020.07.08 |
Firebase Cloud Messaging 용 API KEY는 어디에서 찾을 수 있습니까? (0) | 2020.07.08 |