Skip to content

Commit f219ae8

Browse files
committed
Fixed Android measurement issue
1 parent 976d260 commit f219ae8

File tree

1 file changed

+93
-93
lines changed

1 file changed

+93
-93
lines changed

src/CircularSlider.js

Lines changed: 93 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { PureComponent, PropTypes } from 'react';
2-
import { PanResponder } from 'react-native';
2+
import { PanResponder, View } from 'react-native';
33
import Svg, { Circle, G, LinearGradient, Path, Defs, Stop } from 'react-native-svg';
44
import range from 'lodash.range';
55
import { interpolateHcl as interpolateGradient } from 'd3-interpolate';
@@ -82,7 +82,6 @@ export default class CircularSlider extends PureComponent {
8282
this._sleepPanResponder = PanResponder.create({
8383
onMoveShouldSetPanResponder: (evt, gestureState) => true,
8484
onMoveShouldSetPanResponderCapture: (evt, gestureState) => true,
85-
onPanResponderGrant: this.setCircleCenter,
8685

8786
onPanResponderMove: (evt, { moveX, moveY }) => {
8887
const { circleCenterX, circleCenterY } = this.state;
@@ -108,7 +107,6 @@ export default class CircularSlider extends PureComponent {
108107
this._wakePanResponder = PanResponder.create({
109108
onMoveShouldSetPanResponder: (evt, gestureState) => true,
110109
onMoveShouldSetPanResponderCapture: (evt, gestureState) => true,
111-
onPanResponderGrant: this.setCircleCenter,
112110

113111
onPanResponderMove: (evt, { moveX, moveY }) => {
114112
const { circleCenterX, circleCenterY } = this.state;
@@ -126,12 +124,12 @@ export default class CircularSlider extends PureComponent {
126124
});
127125
}
128126

129-
componentDidMount() {
127+
onLayout = () => {
130128
this.setCircleCenter();
131129
}
132130

133131
setCircleCenter = () => {
134-
this._circle.root.measure((x, y, w, h, px , py) => {
132+
this._circle.measure((x, y, w, h, px , py) => {
135133
const halfOfContainer = this.getContainerWidth() / 2;
136134
this.setState({ circleCenterX: px + halfOfContainer, circleCenterY: py + halfOfContainer });
137135
});
@@ -152,105 +150,107 @@ export default class CircularSlider extends PureComponent {
152150
const stop = calculateArcCircle(segments - 1, segments, radius, startAngle, angleLength);
153151

154152
return (
155-
<Svg
156-
height={containerWidth}
157-
width={containerWidth}
158-
>
159-
<Defs>
160-
{
161-
range(segments).map(i => {
162-
const { fromX, fromY, toX, toY } = calculateArcCircle(i, segments, radius, startAngle, angleLength);
163-
const { fromColor, toColor } = calculateArcColor(i, segments, gradientColorFrom, gradientColorTo)
164-
return (
165-
<LinearGradient key={i} id={getGradientId(i)} x1={fromX.toFixed(2)} y1={fromY.toFixed(2)} x2={toX.toFixed(2)} y2={toY.toFixed(2)}>
166-
<Stop offset="0%" stopColor={fromColor} />
167-
<Stop offset="1" stopColor={toColor} />
168-
</LinearGradient>
169-
)
170-
})
171-
}
172-
</Defs>
173-
174-
{/*
175-
##### Circle
176-
*/}
177-
178-
<G transform={{ translate: `${strokeWidth/2 + radius + 1}, ${strokeWidth/2 + radius + 1}` }}>
179-
<Circle
180-
r={radius}
181-
strokeWidth={strokeWidth}
182-
fill="transparent"
183-
stroke={bgCircleColor}
184-
ref={circle => this._circle = circle}
185-
/>
186-
{
187-
showClockFace && (
188-
<ClockFace
189-
r={radius - strokeWidth / 2}
190-
stroke={clockFaceColor}
191-
/>
192-
)
193-
}
194-
{
195-
range(segments).map(i => {
196-
const { fromX, fromY, toX, toY } = calculateArcCircle(i, segments, radius, startAngle, angleLength);
197-
const d = `M ${fromX.toFixed(2)} ${fromY.toFixed(2)} A ${radius} ${radius} 0 0 1 ${toX.toFixed(2)} ${toY.toFixed(2)}`;
198-
199-
return (
200-
<Path
201-
d={d}
202-
key={i}
203-
strokeWidth={strokeWidth}
204-
stroke={`url(#${getGradientId(i)})`}
205-
fill="transparent"
206-
/>
207-
)
208-
})
209-
}
210-
211-
{/*
212-
##### Stop Icon
213-
*/}
214-
215-
<G
216-
fill={gradientColorTo}
217-
transform={{ translate: `${stop.toX}, ${stop.toY}` }}
218-
onPressIn={() => this.setState({ angleLength: angleLength + Math.PI / 2 })}
219-
{...this._wakePanResponder.panHandlers}
220-
>
221-
<Circle
222-
r={(strokeWidth - 1) / 2}
223-
fill={bgCircleColor}
224-
stroke={gradientColorTo}
225-
strokeWidth="1"
226-
/>
153+
<View style={{ width: containerWidth, height: containerWidth }} onLayout={this.onLayout}>
154+
<Svg
155+
height={containerWidth}
156+
width={containerWidth}
157+
ref={circle => this._circle = circle}
158+
>
159+
<Defs>
227160
{
228-
stopIcon
161+
range(segments).map(i => {
162+
const { fromX, fromY, toX, toY } = calculateArcCircle(i, segments, radius, startAngle, angleLength);
163+
const { fromColor, toColor } = calculateArcColor(i, segments, gradientColorFrom, gradientColorTo)
164+
return (
165+
<LinearGradient key={i} id={getGradientId(i)} x1={fromX.toFixed(2)} y1={fromY.toFixed(2)} x2={toX.toFixed(2)} y2={toY.toFixed(2)}>
166+
<Stop offset="0%" stopColor={fromColor} />
167+
<Stop offset="1" stopColor={toColor} />
168+
</LinearGradient>
169+
)
170+
})
229171
}
230-
</G>
172+
</Defs>
231173

232174
{/*
233-
##### Start Icon
175+
##### Circle
234176
*/}
235177

236-
<G
237-
fill={gradientColorFrom}
238-
transform={{ translate: `${start.fromX}, ${start.fromY}` }}
239-
onPressIn={() => this.setState({ startAngle: startAngle - Math.PI / 2, angleLength: angleLength + Math.PI / 2 })}
240-
{...this._sleepPanResponder.panHandlers}
241-
>
178+
<G transform={{ translate: `${strokeWidth/2 + radius + 1}, ${strokeWidth/2 + radius + 1}` }}>
242179
<Circle
243-
r={(strokeWidth - 1) / 2}
244-
fill={bgCircleColor}
245-
stroke={gradientColorFrom}
246-
strokeWidth="1"
180+
r={radius}
181+
strokeWidth={strokeWidth}
182+
fill="transparent"
183+
stroke={bgCircleColor}
247184
/>
248185
{
249-
startIcon
186+
showClockFace && (
187+
<ClockFace
188+
r={radius - strokeWidth / 2}
189+
stroke={clockFaceColor}
190+
/>
191+
)
250192
}
193+
{
194+
range(segments).map(i => {
195+
const { fromX, fromY, toX, toY } = calculateArcCircle(i, segments, radius, startAngle, angleLength);
196+
const d = `M ${fromX.toFixed(2)} ${fromY.toFixed(2)} A ${radius} ${radius} 0 0 1 ${toX.toFixed(2)} ${toY.toFixed(2)}`;
197+
198+
return (
199+
<Path
200+
d={d}
201+
key={i}
202+
strokeWidth={strokeWidth}
203+
stroke={`url(#${getGradientId(i)})`}
204+
fill="transparent"
205+
/>
206+
)
207+
})
208+
}
209+
210+
{/*
211+
##### Stop Icon
212+
*/}
213+
214+
<G
215+
fill={gradientColorTo}
216+
transform={{ translate: `${stop.toX}, ${stop.toY}` }}
217+
onPressIn={() => this.setState({ angleLength: angleLength + Math.PI / 2 })}
218+
{...this._wakePanResponder.panHandlers}
219+
>
220+
<Circle
221+
r={(strokeWidth - 1) / 2}
222+
fill={bgCircleColor}
223+
stroke={gradientColorTo}
224+
strokeWidth="1"
225+
/>
226+
{
227+
stopIcon
228+
}
229+
</G>
230+
231+
{/*
232+
##### Start Icon
233+
*/}
234+
235+
<G
236+
fill={gradientColorFrom}
237+
transform={{ translate: `${start.fromX}, ${start.fromY}` }}
238+
onPressIn={() => this.setState({ startAngle: startAngle - Math.PI / 2, angleLength: angleLength + Math.PI / 2 })}
239+
{...this._sleepPanResponder.panHandlers}
240+
>
241+
<Circle
242+
r={(strokeWidth - 1) / 2}
243+
fill={bgCircleColor}
244+
stroke={gradientColorFrom}
245+
strokeWidth="1"
246+
/>
247+
{
248+
startIcon
249+
}
250+
</G>
251251
</G>
252-
</G>
253-
</Svg>
252+
</Svg>
253+
</View>
254254
);
255255
}
256256
}

0 commit comments

Comments
 (0)