I am doing react native project. In that, I have to show custom image like toggle button, In that, for OFF one image and ON another image I have to display, And in that, two components should have to display according to ON/OFF states.
I am new to this domain.
I have knowledge to set image touchable/Onpress, but, How to set custom image and act like toggle according to that components switch.
this.toggleAction() = () => {
//switching components for ON/OFF states
}
<TouchableHighlight >
<Image style={styles.imagestyle}
source={require('./ic_toggle_on.png')} />
onPress={() => this.toggleAction()}>
</TouchableHighlight>
Any suggestions?
You need to store the current state of Toggle button in a state variable:
this.state={
toggle:false
}
then you need to change the state in the onPress method of your TouchableOpacity.
After that you just need conditional rendering to show the different images
render(
<TouchableOpacity onPress={()=>this.setState({toggle:!this.state.toggle})}>
{
this.state.toggle==true?
<Image src={ YOUR TOGGLE ON SOURCE}/>
:
<Image src={ YOUR TOGGLE Off SOURCE}/>
}
)
I have a Nativescript Vue component template defined as:
<ScrollView>
<StackLayout>
<ContentView ref="mapContainer" height="500" width="100%">
<Mapbox
accessToken="my access key"
mapStyle="outdoors-v9"
hideCompass="true"
zoomLevel="10.2" ,
latitude="51.949266"
longitude="-12.183571"
showUserLocation="true"
disableZoom="true"
disableRotation="true"
disableScroll="true"
disableTilt="true"
#mapReady="onMapReady($event)">
</Mapbox>
</ContentView>
<Label text="A test label"/>
</StackLayout>
</ScrollView>
When the component is mounted, I want to set the height of mapContainer to roughly 75% of the screen height. To do so, I have:
export default {
name: "MyPage",
mounted () {
this.$refs.mapContainer.height = platform.screen.mainScreen.heightDIPs
}
...
}
But this does nothing, and the ContentView remains at 500dp tall.
height is meant to be a setter, but I figure I'm missing a redraw (?) to get this change to take effect, but not sure how?
In order to access the ContentView via refs, you must use .nativeView property.
this.$refs.mapContainer.nativeView.height = platform.screen.mainScreen.heightDIPs
Also you don't have to calculate the height but simply set the height in percentage (70%) instead of fixed value (500) Or use a GridLayout with 7 partition (7*).
For the past couple of weeks i'm trying to figure out how to include an external web app that does 3d modelling(Through THREE.js) into a viro react app. I tried webview and that works, but i need the model to be imported into my Viro App. I tried the WebViewBridge module (which in theory could send the .obj file as a string to my app from the webview so i can show it in AR) But it doesn't seem to work on the Native react version that Viro uses.
<View >
<WebViewBridge
ref="webviewbridge"
source={{uri: 'http://www.google.com'}}>
</WebViewBridge>
</View>
When i change the "webviewbridge" for "webview" it works, it shows google in a new view if i press a button. My aim is to have the web app shown, and on a button click i can get the presented 3D model and show it in augmented reality (a feature of viro-react).
Technical info:
pc: windows 10
react-native-cli: 2.0.1
react-native: 0.49.3
Viro-react testbed version: 2.4.0
Test device: Samsung s8 (Android)
Webview bridge: https://www.npmjs.com/package/react-native-webview-bridge-updated
My bad, it uses "injectedJavaScript" prop now.
let jsCode = " document.querySelector('#myContent').style.backgroundColor = 'green';";
return(
<View style={localStyles.viroContainer}> //this just sets flex to 1
<WebView
source={{ html: "<h1 id='myContent'>Hello</h1>" }}
style={{ flex: 1 }}
ref={ webview => { this.webview = webview; }}
injectedJavaScript={jsCode}
javaScriptEnabled={true}>
</WebView>
<TouchableHighlight style={localStyles.overlayButton}
onPress={this.sendMessageToWebView2} underlayColor='transparent'>
<Text>Send message to WebView</Text>
</TouchableHighlight>
</View>
);
}
I managed to get an alert with a string in my webview now on the buttonclick (sendmessagetowebview2), But i havent managed to change the injectedJavaScript yet.
I have an image in a .png format with the alpha channel and i wish it to be clickable. The click should be detected only if the real image is clicked (not the alpha background).
<TouchableHighlight onPress={this.imgClick}>
<Image
source={this.img1}
/>
</TouchableHighlight>
And i want to create a clickable circle of these elements
Thank you for helping and i wish you a Merry X-Mas !!
You can detect where on the image has been clicked and then you should have some metadata on the image to detect that it is in the non-alpha bounds.
imgClick = ({nativeEvent: {locationX, locationY}}) => {
if (ImageService.inBounds(image, locationX, locationY)) {
// do your event handling here
}
}
ImageService would be where your metadata and logic is stored for each image. You probably want a simple point in polygon algorithm.
https://en.wikipedia.org/wiki/Point_in_polygon
I am new to appcelerator titanium and have a question
how can I create a modal window that blurs its parent, or have a semi transparent background?, I managed to create a modal window but the parent went black.
thanks in advance
This is the current way to accomplish this in Titanium as of 3.1.3 on iOS.
First, make a new window.
var myModal = Ti.UI.createWindow({
title : 'My Modal',
backgroundColor : 'transparent'
});
Then create a wrapper view, a background view, and a container view:
var wrapperView = Ti.UI.createView(); // Full screen
var backgroundView = Ti.UI.createView({ // Also full screen
backgroundColor : '#000',
opacity : 0.5
});
var containerView = Ti.UI.createView({ // Set height appropriately
height : 300,
backgroundColor : '#FFF'
});
var someLabel = Ti.UI.createLabel({
title : 'Here is your modal',
top : 40
});
var closeButton = Ti.UI.createButton({
title : 'Close',
bottom : 40
});
closeButton.addEventListener('click', function () {
myModal.close();
});
Now build your UI stack. The order is important to avoid having to set z-index.
containerView.add(someLabel);
containerView.add(closeButton);
wrapperView.add(backgroundView);
wrapperView.add(containerView);
myModal.add(wrapperView);
Now you can open your modal, but to NOT set modal : true
myModal.open({
animate : true
});
Its very simple.Just create the window, and when you're opening it,specify the 'modal' property as true!
var ModalWindow = Ti.UI.createWindow({});
ModalWindow.open({modal:true});
In Titanium Appcelerator (tried in 1.6.2) a modal window is always a full screen window. The parent might seem black because this modal window's background is black.
Try specifying a semi-transparent image as the background of this modal window, you are creating and you might get the effect you want from it.
You could try using the opacity on the window you have that is overlaying the other.
Ti.UI.currentWindow.opacity = 0.4;
If you are on iPhone, probably you cannot make it. on iPhone, if a modal dialog appears, the other window on the render stack will be cleared. That is, only ONE modal dialog in the render stack. That's the reason you got black if other areas of the parent are not covered by your modal window. iPad implemented modal dialog using "sheet" style, so you can make the are areas semi transparent.
I like the solution presented by AlienWebguy, although I think there's a minor bug. When you create your label, I think you meant to set the text property and not the title property:
var someLabel = Ti.UI.createLabel({
text: 'Here is your modal',
/* etc., etc. */
});
When I used title, it (the label) was not appearing in the window.
Another modification I might make is to set the layout property for the container view, e.g.,
var containerView = Ti.UI.createView({
height: 100, /* or whatever is appropriate */
backgroundColor : '#FFF',
layout: 'vertical'
});
In doing this, you can "stack" up the GUI elements in that view and not worry (too much) about setting layout coordinates... At least that's what I do in creating a customized alert box using the techniques outlined here.
i was also looking for such window with semi transparent background for ios 8.4. I tried the way "AlienWebguy" in Alloy XMl but the issue was whole window getting opacity 0.5 and background stacked window content are clearly visible than foreground view content. i have did some changes to the "AlienWebguy" to get the required result:
<Alloy>
<Window backgroundColor="transparent" modal="false">
<View layout="vertical" width="Ti.UI.FILL" height="Ti.UI.FILL" backgroundColor="#000" opacity="0.5">
// View will fill whole window with transparent shade of black color.
</View>
<View class="container" zIndex="100" height="400" width="Ti.UI.FILL" backgroundColor="#fff">
// You can add any content here, which will be look like Modal window.
//View automatically vertically centered on screen.
</View>
</Window>
</Alloy>
Hope this will save the time of developer doing in Alloy. Thanks for "AlienWebguy" for the concept.
why not just give it transparent background?
<Window backgroundColor="#80000000">
<View class="container">
// Your views
</View>
</Window>