2017-06-27 24 views
11

私はデザイナーのデザインに基づいてReact Nativeアプリを開発しています。デザインには、1つの対角線を持つボタンや図形がある場所がいくつかあります(次の例を参照)。私はSkewXを使用しようとしましたが、それはちょうど全体の形を回転させるようです(とにかくAndroidでは動作しないようです)。私はどのように四角形/ボタンを片側に斜めの境界線を描くことができますか?React Nativeで対角の枠線を作成するにはどうすればよいですか?

button with diagonal border

+1

http://browniefed.com/blog/the-shapes-of-react-native/は平行四辺形までスクロールします。 –

答えて

15

あなたはViewクラスにCSSを適用し、所望の出力を作成することができ、 相続人の小さなデモコード編集されたバージョン

import React, { Component } from 'react'; 
import { View, StyleSheet } from 'react-native'; 
import { Constants } from 'expo'; 

export default class App extends Component { 
    render() { 
    return (
     <View style={styles.container}> 

     <View style={styles.triangleCorner}></View> 
     <View style={styles.triangleCornerLayer}></View> 
     <View style={styles.triangleCorner1}></View> 

     </View> 
    ); 
    } 
} 

const styles = StyleSheet.create({ 
    container: { 
    flex: 1, 
    alignItems: 'center', 
    justifyContent: 'center', 
    paddingTop: Constants.statusBarHeight, 
    backgroundColor: '#ecf0f1', 
    },triangleCorner: { 
    position: 'absolute', 
    top:105, 
    left:0, 
    width: 300, 
    height: 100, 
    backgroundColor: 'transparent', 
    borderStyle: 'solid', 
    borderRightWidth: 50, 
    borderTopWidth: 80, 
    borderRightColor: 'transparent', 
    borderTopColor: 'gray' 
    },triangleCorner1: { 
    position: 'absolute', 
    top:100, 
    left:0, 
    width: 130, 
    backgroundColor: 'transparent', 
    borderStyle: 'solid', 
    borderRightWidth: 50, 
    borderTopWidth: 90, 
    borderRightColor: 'transparent', 
    borderTopColor: 'green' 
    },triangleCornerLayer: { 
    position: 'absolute', 
    top:107, 
    left:0, 
    width:297, 
    height: 100, 
    backgroundColor: 'transparent', 
    borderStyle: 'solid', 
    borderRightWidth: 47, 
    borderTopWidth: 75, 
    borderRightColor: 'transparent', 
    borderTopColor: 'white' 
    } 
}); 

結果:

enter image description here

+0

ありがとう!これは本当に面白いです。オリジナルのポストのデザインでは、ボタンの2番目の部分(あなたの灰色の部分)は、実際には白で、灰色の境界線があります。このメソッドは、実際に斜めのエッジを作成するために境界の使用を必要とするので、どのように境界を達成するのですか?白い背景を持つ2ポイントの背の低いものと1ポイント少ないものを除き、それを灰色のものの上に重ねる以外は、同じ「triangleCorner1」を作成する必要がありますか?本当に退屈なようです! > _ < – user2719094

+0

私はちょっと退屈だと知っていますが、オーバーレイは私が知っている唯一のオプションです –

0

を使用そのような形状のための。そのためのコードの下

let layer = CAShapeLayer() 
    let path = UIBezierPath() 
    path.move(to: CGPoint(x: 0, y: 0)) 
    path.addLine(to: CGPoint(x: 150, y: 0)) 
    path.addLine(to: CGPoint(x: 100, y: 50)) 
    path.addLine(to: CGPoint(x: 0, y: 50)) 
    path.close() 
    layer.path = path.cgPath 
    layer.fillColor = UIColor.green.cgColor 
    layer.strokeColor = UIColor.clear.cgColor 
    view.layer.addSublayer(layer) 

    let layer1 = CAShapeLayer() 
    path.move(to: CGPoint(x: 100, y: 45)) 
    path.addLine(to: CGPoint(x: 300, y: 45)) 
    path.addLine(to: CGPoint(x: 350, y: 5)) 
    path.addLine(to: CGPoint(x: 150, y: 5)) 
    path.close() 
    layer1.path = path.cgPath 
    layer1.fillColor = UIColor.clear.cgColor 
    layer1.strokeColor = UIColor.black.cgColor 
    view.layer.addSublayer(layer1) 
+1

この質問はReact Nativeについてです。このコードはSwiftと思われる。 – user2719094

関連する問題