Skip to content

Commit 991a064

Browse files
committed
Adding support for on press camera
1 parent 1bf031e commit 991a064

File tree

2 files changed

+74
-48
lines changed

2 files changed

+74
-48
lines changed

index.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ export default class Camera extends Component {
100100
barcodeScannerEnabled: PropTypes.bool,
101101
onFocusChanged: PropTypes.func,
102102
onZoomChanged: PropTypes.func,
103+
onPressCamera: PropTypes.func,
103104
mirrorImage: PropTypes.bool,
104105
fixOrientation: PropTypes.bool,
105106
barCodeTypes: PropTypes.array,
@@ -155,6 +156,7 @@ export default class Camera extends Component {
155156

156157
async componentWillMount() {
157158
this._addOnBarCodeReadListener()
159+
this._addOnPressCameraListener()
158160

159161
let { captureMode } = convertNativeProps({ captureMode: this.props.captureMode })
160162
let hasVideoAndAudio = this.props.captureAudio && captureMode === Camera.constants.CaptureMode.video
@@ -167,6 +169,7 @@ export default class Camera extends Component {
167169
}
168170

169171
componentWillUnmount() {
172+
this._removeOnPressCameraListener()
170173
this._removeOnBarCodeReadListener()
171174

172175
if (this.state.isRecording) {
@@ -175,10 +178,13 @@ export default class Camera extends Component {
175178
}
176179

177180
componentWillReceiveProps(newProps) {
178-
const { onBarCodeRead } = this.props
181+
const { onBarCodeRead, onPressCamera } = this.props
179182
if (onBarCodeRead !== newProps.onBarCodeRead) {
180183
this._addOnBarCodeReadListener(newProps)
181184
}
185+
if (onPressCamera !== newProps.onPressCamera) {
186+
this._addOnPressCameraListener(newProps)
187+
}
182188
}
183189

184190
_addOnBarCodeReadListener(props) {
@@ -198,6 +204,22 @@ export default class Camera extends Component {
198204
}
199205
}
200206

207+
_addOnPressCameraListener(props) {
208+
const { onPressCamera } = props || this.props
209+
this._removeOnPressCameraListener()
210+
if (onPressCamera) {
211+
this.onPressCameraListener = Platform.select({
212+
ios: NativeAppEventEmitter.addListener('onPressCamera', this._onPressCamera.bind(this)),
213+
})
214+
}
215+
}
216+
_removeOnPressCameraListener() {
217+
const listener = this.onPressCameraListener
218+
if (listener) {
219+
listener.remove()
220+
}
221+
}
222+
201223
render() {
202224
const style = [styles.base, this.props.style];
203225
const nativeProps = convertNativeProps(this.props);
@@ -211,6 +233,12 @@ export default class Camera extends Component {
211233
}
212234
};
213235

236+
_onPressCamera(event) {
237+
if (this.props.onPressCamera) {
238+
this.props.onPressCamera(event)
239+
}
240+
}
241+
214242
capture(options) {
215243
const props = convertNativeProps(this.props);
216244
options = {
@@ -269,7 +297,7 @@ const RCTCamera = requireNativeComponent('RCTCamera', Camera, {nativeOnly: {
269297
importantForAccessibility: true,
270298
accessibilityLiveRegion: true,
271299
accessibilityComponentType: true,
272-
onLayout: true
300+
onLayout: true,
273301
}});
274302

275303
const styles = StyleSheet.create({

ios/RCTCamera.m

Lines changed: 44 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ - (void)layoutSubviews
9090
self.manager.previewLayer.frame = self.bounds;
9191
}
9292
[self setBackgroundColor:[UIColor blackColor]];
93-
NSLog(@"sublayers count: %d", [self.layer.sublayers count]);
9493
if ([self.layer.sublayers count] == 0) {
9594
[self.layer insertSublayer:self.manager.previewLayer atIndex:0];
9695
}
@@ -123,59 +122,58 @@ - (void)orientationChanged:(NSNotification *)notification{
123122
}
124123

125124

126-
- (void) touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
125+
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
127126
{
128-
// Update the touch state.
129-
if ([[event touchesForView:self] count] > 1) {
130-
_multipleTouches = YES;
131-
}
132-
127+
// Update the touch state.
128+
if ([[event touchesForView:self] count] > 1) {
129+
_multipleTouches = YES;
130+
}
133131
}
134132

135133
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
136134
{
137-
if (!_onFocusChanged) return;
138-
139-
BOOL allTouchesEnded = ([touches count] == [[event touchesForView:self] count]);
140-
141-
// Do not conflict with zooming and etc.
142-
if (allTouchesEnded && !_multipleTouches) {
143-
UITouch *touch = [[event allTouches] anyObject];
144-
CGPoint touchPoint = [touch locationInView:touch.view];
145-
// Focus camera on this point
146-
[self.manager focusAtThePoint:touchPoint];
147-
148-
if (self.camFocus)
149-
{
150-
[self.camFocus removeFromSuperview];
151-
}
152-
NSDictionary *event = @{
153-
@"target": self.reactTag,
154-
@"touchPoint": @{
155-
@"x": [NSNumber numberWithDouble:touchPoint.x],
156-
@"y": [NSNumber numberWithDouble:touchPoint.y]
157-
}
158-
};
159-
[self.bridge.eventDispatcher sendInputEventWithName:@"focusChanged" body:event];
160-
161-
// Show animated rectangle on the touched area
162-
if (_defaultOnFocusComponent) {
163-
self.camFocus = [[RCTCameraFocusSquare alloc]initWithFrame:CGRectMake(touchPoint.x-40, touchPoint.y-40, 80, 80)];
164-
[self.camFocus setBackgroundColor:[UIColor clearColor]];
165-
[self addSubview:self.camFocus];
166-
[self.camFocus setNeedsDisplay];
167-
168-
[UIView beginAnimations:nil context:NULL];
169-
[UIView setAnimationDuration:1.0];
170-
[self.camFocus setAlpha:0.0];
171-
[UIView commitAnimations];
172-
}
173-
}
135+
if (!_onFocusChanged && false) return;
136+
137+
BOOL allTouchesEnded = ([touches count] == [[event touchesForView:self] count]);
174138

175-
if (allTouchesEnded) {
176-
_multipleTouches = NO;
139+
// Do not conflict with zooming and etc.
140+
if (allTouchesEnded && !_multipleTouches) {
141+
UITouch *touch = [[event allTouches] anyObject];
142+
CGPoint touchPoint = [touch locationInView:touch.view];
143+
// Focus camera on this point
144+
[self.manager focusAtThePoint:touchPoint];
145+
146+
if (self.camFocus)
147+
{
148+
[self.camFocus removeFromSuperview];
149+
}
150+
NSDictionary *event = @{
151+
@"target": self.reactTag,
152+
@"touchPoint": @{
153+
@"x": [NSNumber numberWithDouble:touchPoint.x],
154+
@"y": [NSNumber numberWithDouble:touchPoint.y]
155+
}
156+
};
157+
[self.bridge.eventDispatcher sendAppEventWithName:@"onPressCamera" body:event];
158+
[self.bridge.eventDispatcher sendInputEventWithName:@"focusChanged" body:event];
159+
160+
// Show animated rectangle on the touched area
161+
if (_defaultOnFocusComponent) {
162+
self.camFocus = [[RCTCameraFocusSquare alloc] initWithFrame:CGRectMake(touchPoint.x-40, touchPoint.y-40, 80, 80)];
163+
[self.camFocus setBackgroundColor:[UIColor clearColor]];
164+
[self addSubview:self.camFocus];
165+
[self.camFocus setNeedsDisplay];
166+
167+
[UIView beginAnimations:nil context:NULL];
168+
[UIView setAnimationDuration:1.0];
169+
[self.camFocus setAlpha:0.0];
170+
[UIView commitAnimations];
177171
}
172+
}
178173

174+
if (allTouchesEnded) {
175+
_multipleTouches = NO;
176+
}
179177
}
180178

181179

0 commit comments

Comments
 (0)